经典 SQL

经典sql
 
总结一些经常用到或碰到的SQL语句,希望能与大家分享,同时也希望大家能提供更多的精妙SQL语句.....

1、delete table1 from (select * from table2) as t2 where table1.id=t2.id

2、truncate table table1 (不在事务日志中做记录,比delete table快,但不能激活触发器)

3、update table1 set column=column+1 where id=(select id from table2)

4、update table1 set column=column+1 from table1,table2 where table1.id=table2.id

5、select top n [Percent] * from table1 '输出百分比记录

6、select id,column1 * column2 as column from table1 '可算明白as的用法了

7、select * from table1 where column1 like 'SQL#_G_O' escape '#' '单匹配

8、select table1.id from table1 where not exists (select table2.id from table2 where table1.id=table2.id) '这个应该比not in快一些

9、select table1.id from table1,table2 where table1.id<>table2.id '看复合查询机制

10、select table1.id from table1,table2,(select id from table3) as t3 where table1.id=table2.id and table2.id=t3.id '有些类似[1]了......

11、select * from table1 where column1 like '[A]%' or like '[^B]%'

12、select @column1=column1 from table1;select @column1 as column1 '存储到自定义变量

13、select * from table1 where contains(column1,'char1 or char2*') '全文索引

14、select * from table1 where contains(column1,'前有 near 中有 near 后有')

15、select * from table1 where contains(column1,'formsof(inflectional,go)') '派生

16、select * from table1 where contains(description,'isabout(apple weight(.9),boy weight(.8),china weight(.7))') '权重

17、select * from table1 where freetext(column1,'char') '仅支持文字不支持表达式搜索

18、insert into table1 select column1,count(column1) from table2 group by column1 '统计



-----------------------------------------------------------------------------------------

1 说明:复制表(只复制结构,源表名:a 新表名:b)

SQL: select * into b from a where 1<>1



2 说明:拷贝表(拷贝数据,源表名:a 目标表名:b)

SQL: insert into b(a, b, c) select d,e,f from b;



3 说明:显示文章、提交人和最后回复时间

SQL: select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b



4 说明:外连接查询(表名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



5 说明:日程安排提前五分钟提醒

SQL:  select * from 日程安排 where datediff('minute',f开始时间,getdate())>5





6 说明:两张关联表,删除主表中已经在副表中没有的信息

SQL:  

delete from info where not exists ( select * from infobz where info.infid=infobz.infid ) 



7 说明:

从数据库中去一年的各单位电话费统计(电话费定额和电话费清单两个表来源)

SQL: 

SELECT a.userper, a.tel, a.standfee, TO_CHAR(a.telfeedate, 'yyyy') AS telyear,

      SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '01', a.factration)) AS JAN,

      SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '02', a.factration)) AS FRI,

      SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '03', a.factration)) AS MAR,

      SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '04', a.factration)) AS APR,

      SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '05', a.factration)) AS MAY,

      SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '06', a.factration)) AS JUE,

      SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '07', a.factration)) AS JUL,

      SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '08', a.factration)) AS AGU,

      SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '09', a.factration)) AS SEP,

      SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '10', a.factration)) AS OCT,

      SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '11', a.factration)) AS NOV,

      SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '12', a.factration)) AS DEC

FROM (SELECT a.userper, a.tel, a.standfee, b.telfeedate, b.factration

        FROM TELFEESTAND a, TELFEE b

        WHERE a.tel = b.telfax) a

GROUP BY a.userper, a.tel, a.standfee, TO_CHAR(a.telfeedate, 'yyyy')



8 说明:四表联查问题:

SQL: select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c  inner join d on a.a=d.d where .....



9 说明:得到表中最小的未使用的ID号

SQL:

SELECT (CASE WHEN EXISTS(SELECT * FROM Handle b WHERE b.HandleID = 1) THEN MIN(HandleID) + 1 ELSE 1 END) as HandleID

FROM  Handle

WHERE NOT HandleID IN (SELECT a.HandleID - 1 FROM Handle a)



10 说明:模糊查询,单字匹配(短横线代表待匹配内容)



select * from table where field1 like 'A_B_C'



11 说明:as的用法



select id,column1 * column2 as column from table1
原文地址:https://www.cnblogs.com/wyy226wyj/p/3623643.html