acerbic coffee
          走自己的路,讓別人跑步
          posts - 26,comments - 14,trackbacks - 0
          總結一些工作中用到或碰到的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

          posted on 2006-10-24 14:23 acerbic coffee 閱讀(6923) 評論(1)  編輯  收藏

          FeedBack:
          # re: 無意中查sql中as的用法搜索到的一些經典的sql語句
          2008-10-20 13:02 | 孤獨一夏
          在下非常感謝你提供的這些資料,我一下解決了長久以來無法解決的問題。
          對我很有幫助。
          (我找了很久都沒有找到。)
          在此謝過  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 盐源县| 尉氏县| 来凤县| 罗定市| 嵊州市| 资兴市| 武义县| 海安县| 禹城市| 营口市| 达尔| 三都| 永川市| 乃东县| 内黄县| 金秀| 申扎县| 枣阳市| 巴彦淖尔市| 潞城市| 许昌市| 武川县| 蒙阴县| 那曲县| 孟村| 新巴尔虎左旗| 澄城县| 怀来县| 榆社县| 博爱县| 全州县| 洪江市| 腾冲县| 澎湖县| 滦平县| 蓬安县| 驻马店市| 会同县| 依兰县| 鸡西市| 北宁市|