【文章內(nèi)容簡(jiǎn)介】
’值6’)說明:兩張關(guān)聯(lián)表,刪除主表中已經(jīng)在副表中沒有的信息delete from table1 where not exists ( select * from table2 where =1說明:四表聯(lián)查問題:select * from a left inner join b on = right inner join c on = inner join d on = where .....1說明:日程安排提前五分鐘提醒SQL: select * from 日程安排 where datediff(‘minute‘,f開始時(shí)間,getdate())51說明:一條sql 語(yǔ)句搞定數(shù)據(jù)庫(kù)分頁(yè)select top 10 b.* from (select top 20 主鍵字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where = order by 1說明:前10條記錄select top 10 * form table1 where 范圍1說明:選擇在每一組b值相同的數(shù)據(jù)中對(duì)應(yīng)的a最大的記錄的所有信息(類似這樣的用法可以用于論壇每月排行榜,每月熱銷產(chǎn)品分析,按科目成績(jī)排名,等等.)select a,b,c from tablename ta where a=(select max(a) from tablename tb where =)1說明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重復(fù)行而派生出一個(gè)結(jié)果表(select a from tableA except (select a from tableB) except (select a from tableC)1說明:隨機(jī)取出10條數(shù)據(jù)select top 10 * from tablename order by newid()1說明:隨機(jī)選擇記錄select newid()1說明:刪除重復(fù)記錄Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)說明:列出數(shù)據(jù)庫(kù)里所有的表名select name from sysobjects where type=‘U‘2說明:列出表里的所有的select name from syscolumns where id=object_id(‘TableName‘)2說明:列示type、vender、pcs字段,以type字段排列,case可以方便地實(shí)現(xiàn)多重選擇,類似select 中的case。select type,sum(case vender when ‘A‘ then pcs else 0 end),sum(case vender when ‘C‘ then pcs else 0 end),sum(case vender when ‘B‘ then pcs else 0 end) FROM tablename group by type顯示結(jié)果:type vender pcs電腦 A 1電腦 A 1光盤 B 2光盤 A 2手機(jī) B 3手機(jī) C 32說明:初始化表table1TRUNCATE TABLE table12說明:選擇從10到15的記錄select top 5 * from (select top 15 * from table order by id asc) table_別名 order by id desc三、技巧1=1,1=2的使用