文章类型: DATABASE
关键词: 快速,掌握,精妙,SQL,server
内容摘要:

教你快速掌握一些异常精妙的"SQL"语句

2015/7/29 14:32:54    来源:apple    阅读:

精妙的"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') &brvbar;&brvbar; '/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
↑ 上一篇文章:sql语句妙用,各种sql语句的详细用法与讲解 关键词:sql,server,database,详细,用法,讲解 发布日期:2015/7/29 14:31:53
↓ 下一篇文章:三种数据库利用SQL语句进行高效果分页 关键词:数据库,SQL,高效,分页,oracle,access 发布日期:2015/7/29 14:33:44
相关文章:
SQL Server附加数据库失败,错误5120问题解决办法 关键词:SQL,Server附加数据库失败,错误5120问题解决办法 发布日期:2017-01-04 20:48
Sql Server能不能把数据库关系图生成一张图片 关键词:Sql,Server能不能把数据库关系图生成一张图片 发布日期:2016-11-04 11:42
深入讲解SQL Union和Union All的使用方法 关键词:SQL,sql,server,Union,Union,All,使用方法 发布日期:2015-07-29 14:36
相关目录:.NETDATABASE软件开发JAVAANDROID
我要评论
正在加载评论信息......