精妙的"SQL"语句:
◆复制表(只复制结构,源表名:a 新表名:b)
SQL: select * into b from a where 1<>1
◆拷贝表(拷贝数据,源表名:a 目标表名:b)
SQL: insert into b(a, b, c) select d,e,f from b;
◆显示文章、提交人和最后回复时间
SQL: select a.title,a.username,b.adddate from table a,
(select max(adddate) adddate from table where table.title=a.title) b
◆说明:外连接查询(表名1:a 表名2:b)
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a =
b.c
◆日程安排提前五分钟提醒
SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5
◆两张关联表,删除主表中已经在副表中没有的信息
SQL:
delete from info where not exists
( select * from infobz where info.infid=infobz.infid )
◆说明:
SQL:
SELECT A.NUM, A.NAME, B.UPD_DATE, B.PREV_UPD_DATE
FROM TABLE1,(SELECT X.NUM, X.UPD_DATE, Y.UPD_DATE
PREV_UPD_DATE FROM (SELECT NUM, UPD_DATE, INBOUND_QTY,
STOCK_ONHAND FROM TABLE2 WHERE TO_CHAR(UPD_DATE,
'YYYY/MM') = TO_CHAR(SYSDATE, 'YYYY/MM')) X,
(SELECT NUM, UPD_DATE, STOCK_ONHAND FROM TABLE2
WHERE TO_CHAR(UPD_DATE,'YYYY/MM') = TO_CHAR(TO_DATE
(TO_CHAR(SYSDATE, 'YYYY/MM') ¦¦ '/01','
YYYY/MM/DD') - 1, 'YYYY/MM') ) Y, WHERE X.NUM = Y.NUM
(+)AND X.INBOUND_QTY + NVL(Y.STOCK_ONHAND,0) <>
X.STOCK_ONHAND ) B WHERE A.NUM = B.NUM
◆说明:
SQL:
select * from studentinfo where not exists(select * from student where
studentinfo.id=student.id) and 系名称='"&strdepartmentname&"' and 专业名称
='"&strprofessionname&"' order by 性别,生源地,高考总成绩
实例讲解SQL Server中"Update"的用法
发布时间:2008.02.28 05:07 来源:赛迪网 作者:Alizze
SQL Server中"Update"的用法:
例子:
在表中有两个字段:id_no (varchar) , in_date (datetime) ,把in_date相同的记录的in_date依次累加1秒, 使in_date没有相同的记录。
以下为原始的数据:
id_no in_date
5791 2003-9-1 14:42:02
5792 2003-9-1 14:42:02
5794 2003-9-1 14:42:02
5795 2003-9-1 14:42:03
5796 2003-9-1 14:42:03
5797 2003-9-1 14:42:03
5831 2003-9-1 14:42:04
5832 2003-9-1 14:42:14
5833 2003-9-1 14:42:14
结果为:
id_no in_date
5791 2003-9-1 14:42:02
5792 2003-9-1 14:42:03
5794 2003-9-1 14:42:04
5795 2003-9-1 14:42:05
5796 2003-9-1 14:42:06
5797 2003-9-1 14:42:07
5831 2003-9-1 14:42:08
5832 2003-9-1 14:42:14
5833 2003-9-1 14:42:15
处理的方法:
--建立测试环境
create table a(id_no varchar(8),in_date datetime) go insert into a select '5791','2003-9-1 14:42:02' union all select '5792','2003-9-1 14:42:02' union all select '5794','2003-9-1 14:42:02' union all select '5795','2003-9-1 14:42:03' union all select '5796','2003-9-1 14:42:03' union all select '5797','2003-9-1 14:42:03' union all select '5831','2003-9-1 14:42:04' union all select '5832','2003-9-1 14:42:04' union all select '5833','2003-9-1 14:42:04' union all select '5734','2003-9-1 14:42:02' union all select '6792','2003-9-1 14:42:22' union all select '6794','2003-9-1 14:42:22' union all select '6795','2003-9-1 14:42:23' union all select '6796','2003-9-1 14:42:23' union all select '6797','2003-9-1 14:42:23' union all select '6831','2003-9-1 14:42:34' union all select '6832','2003-9-1 14:42:34' union all select '6833','2003-9-1 14:42:54' union all select '6734','2003-9-1 14:42:22' go --生成临时表,按照in_date排序 select * into # from a order by in_date --相同的时间,加一秒。加完了不带重复的 declare @date1 datetime,@date2 datetime,@date datetime update # set @date=case when @date1=in_date or @date2>=in_date then dateadd(s,1,@date2) else in_date end, @date1=in_date, @date2=@date, in_date=@date --更新到基本表中去 update a set a.in_date=b.in_date from a a join # b on a.id_no=b.id_noselect * from a drop table #,a