sql语句经典用法.doc_第1页
sql语句经典用法.doc_第2页
sql语句经典用法.doc_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

2007-11-30查sql中as的用法搜索到的一些经典的sql语句博客分类: SQLSQLCC+C#Apple总结一些工作中用到或碰到的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.idtable2.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 11 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 Ha

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论