DDL—數(shù)據(jù)定義語(yǔ)言(Create,Alter,Drop,DECLARE) 
DML—數(shù)據(jù)操縱語(yǔ)言(Select,Delete,Update,Insert) 
DCL—數(shù)據(jù)控制語(yǔ)言(GRANT,REVOKE,COMMIT,ROLLBACK) 
首先,簡(jiǎn)要介紹基礎(chǔ)語(yǔ)句: 
1、說(shuō)明:創(chuàng)建數(shù)據(jù)庫(kù) 
Create DATABASE database-name 
2、說(shuō)明:刪除數(shù)據(jù)庫(kù) 
drop database dbname 
3、說(shuō)明:備份sql server 
--- 創(chuàng)建 備份數(shù)據(jù)的 device 
USE master 
EXEC sp_addumpdevice 'disk', 'testBack', 'c:/mssql7backup/MyNwind_1.dat' 
--- 開(kāi)始 備份 
BACKUP DATABASE pubs TO testBack 
4、說(shuō)明:創(chuàng)建新表 
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) 
根據(jù)已有的表創(chuàng)建新表: 
A:create table tab_new like tab_old (使用舊表創(chuàng)建新表) 
B:create table tab_new as select col1,col2… from tab_old definition only 
5、說(shuō)明:刪除新表 
drop table tabname 
6、說(shuō)明:增加一個(gè)列 
Alter table tabname add column col type 
注:列增加后將不能刪除。DB2中列加上后數(shù)據(jù)類型也不能改變,唯一能改變的是增加varchar類型 
的長(zhǎng)度。 
7、說(shuō)明:添加主鍵: Alter table tabname add primary key(col) 
說(shuō)明:刪除主鍵: Alter table tabname drop primary key(col) 
8、說(shuō)明:創(chuàng)建索引:create [unique] index idxname on tabname(col….) 
刪除索引:drop index idxname 
注:索引是不可更改的,想更改必須刪除重新建。 
9、說(shuō)明:創(chuàng)建視圖:create view viewname as select statement 
刪除視圖:drop view viewname 
10、說(shuō)明:幾個(gè)簡(jiǎn)單的基本的sql語(yǔ)句 
選擇:select * from table1 where 范圍 
插入:insert into table1(field1,field2) values(value1,value2) 
刪除:delete from table1 where 范圍 
更新:update table1 set field1=value1 where 范圍 
查找:select * from table1 where field1 like '%value1%' ---like的語(yǔ)法很精妙,查資料! 
排序:select * from table1 order by field1,field2 [desc] 
總數(shù):select count as totalcount from table1 
求和:select sum(field1) as sumvalue from table1 
平均:select avg(field1) as avgvalue from table1 
最大:select max(field1) as maxvalue from table1 
最小:select min(field1) as minvalue from table1 
11、說(shuō)明:幾個(gè)高級(jí)查詢運(yùn)算詞 
A: UNION 運(yùn)算符 
UNION 運(yùn)算符通過(guò)組合其他兩個(gè)結(jié)果表(例如 TABLE1 和 TABLE2)并消去表中任何重復(fù)行而派生 
出一個(gè)結(jié)果表。當(dāng) ALL 隨 UNION 一起使用時(shí)(即 UNION ALL),不消除重復(fù)行。兩種情況下,派 
生表的每一行不是來(lái)自 TABLE1 就是來(lái)自 TABLE2。 
B: EXCEPT 運(yùn)算符 
EXCEPT 運(yùn)算符通過(guò)包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重復(fù)行而派生出一個(gè) 
結(jié)果表。當(dāng) ALL 隨 EXCEPT 一起使用時(shí) (EXCEPT ALL),不消除重復(fù)行。 
C: INTERSECT 運(yùn)算符 
INTERSECT 運(yùn)算符通過(guò)只包括 TABLE1 和 TABLE2 中都有的行并消除所有重復(fù)行而派生出一個(gè)結(jié)果 
表。當(dāng) ALL 隨 INTERSECT 一起使用時(shí) (INTERSECT ALL),不消除重復(fù)行。 
注:使用運(yùn)算詞的幾個(gè)查詢結(jié)果行必須是一致的。 
12、說(shuō)明:使用外連接 
A、left outer join: 
左外連接(左連接):結(jié)果集幾包括連接表的匹配行,也包括左連接表的所有行。 
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 
B:right outer join: 
右外連接(右連接):結(jié)果集既包括連接表的匹配連接行,也包括右連接表的所有行。 
C:full outer join: 
全外連接:不僅包括符號(hào)連接表的匹配行,還包括兩個(gè)連接表中的所有記錄。 
13、說(shuō)明:復(fù)制表(只復(fù)制結(jié)構(gòu),源表名:a 新表名:b) (Access可用) 
法一:select * into b from a where 1<>1 
法二:select top 0 * into b from a 
14、說(shuō)明:拷貝表(拷貝數(shù)據(jù),源表名:a 目標(biāo)表名:b) (Access可用) 
insert into b(a, b, c) select d,e,f from b; 
15、說(shuō)明:跨數(shù)據(jù)庫(kù)之間表的拷貝(具體數(shù)據(jù)使用絕對(duì)路徑) (Access可用) 
insert into b(a, b, c) select d,e,f from b in ‘具體數(shù)據(jù)庫(kù)' where 條件 
例子:..from b in '"&Server.MapPath(".")&"/data.mdb" &"' where.. 
16、說(shuō)明:子查詢(表名1:a 表名2:b) 
select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN 
(1,2,3) 
17、說(shuō)明:顯示文章、提交人和最后回復(fù)時(shí)間 
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from 
table where table.title=a.title) b 
18.說(shuō)明:外連接查詢(表名1:a 表名2:b) 
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c 
19、說(shuō)明:在線視圖查詢(表名1:a ) 
select * from (Select a,b,c FROM a) T where t.a > 1; 
20、說(shuō)明:between的用法,between限制查詢數(shù)據(jù)范圍時(shí)包括了邊界值,not between不包括 
select * from table1 where time between time1 and time2 
select a,b,c, from table1 where a not between 數(shù)值1 and 數(shù)值2 
21、說(shuō)明:in 的使用方法 
select * from table1 where a [not] in (‘值1','值2','值4','值6') 
22、說(shuō)明:兩張關(guān)聯(lián)表,刪除主表中已經(jīng)在副表中沒(méi)有的信息 
delete from table1 where not exists ( select * from table2 where 
table1.field1=table2.field1 ) 
23、說(shuō)明:四表聯(lián)查問(wèn)題: 
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 ..... 
24、說(shuō)明:日程安排提前五分鐘提醒 
sql: select * from 日程安排 where datediff('minute',f開(kāi)始時(shí)間,getdate())>5 
25、說(shuō)明:一條sql 語(yǔ)句搞定數(shù)據(jù)庫(kù)分頁(yè) 
select top 10 b.* from (select top 20 主鍵字段,排序字段 from 表名 order by 排序字段 
desc) a,表名 b where b.主鍵字段 = a.主鍵字段 order by a.排序字段 
26、說(shuō)明:前10條記錄 
select top 10 * form table1 where 范圍 
27、說(shuō)明:選擇在每一組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 
tb.b=ta.b) 
28、說(shuō)明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重復(fù)行而派生出一個(gè) 
結(jié)果表 
(select a from tableA ) except (select a from tableB) except (select a from tableC) 
29、說(shuō)明:隨機(jī)取出10條數(shù)據(jù) 
select top 10 * from tablename order by newid() 
30、說(shuō)明:隨機(jī)選擇記錄 
select newid() 
31、說(shuō)明:刪除重復(fù)記錄 
Delete from tablename where id not in (select max(id) from tablename group by 
col1,col2,...) 
32、說(shuō)明:列出數(shù)據(jù)庫(kù)里所有的表名 
select name from sysobjects where type='U' 
33、說(shuō)明:列出表里的所有的 
select name from syscolumns where id=object_id('TableName') 
34、說(shuō)明:列示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 
光盤(pán) B 2 
光盤(pán) A 2 
手機(jī) B 3 
手機(jī) C 3 
35、說(shuō)明:初始化表table1 
TRUNCATE TABLE table1 
36、說(shuō)明:選擇從10到15的記錄 
select top 5 * from (select top 15 * from table order by id asc) table_別名 order by 
id desc 
   
隨機(jī)選擇數(shù)據(jù)庫(kù)記錄的方法(使用Randomize函數(shù),通過(guò)SQL語(yǔ)句實(shí)現(xiàn)) 
  對(duì)存儲(chǔ)在數(shù)據(jù)庫(kù)中的數(shù)據(jù)來(lái)說(shuō),隨機(jī)數(shù)特性能給出上面的效果,但它們可能太慢了些。你不能 
要求ASP“找個(gè)隨機(jī)數(shù)”然后打印出來(lái)。實(shí)際上常見(jiàn)的解決方案是建立如下所示的循環(huán): 
Randomize 
RNumber = Int(Rnd*499) +1 
  
While Not objRec.EOF 
If objRec("ID") = RNumber THEN 
... 這里是執(zhí)行腳本 ... 
end if 
objRec.MoveNext 
Wend 
  
  這很容易理解。首先,你取出1到500范圍之內(nèi)的一個(gè)隨機(jī)數(shù)(假設(shè)500就是數(shù)據(jù)庫(kù)內(nèi)記錄的總 
數(shù))。然后,你遍歷每一記錄來(lái)測(cè)試ID 的值、檢查其是否匹配RNumber。滿足條件的話就執(zhí)行由 
THEN 關(guān)鍵字開(kāi)始的那一塊代碼。假如你的RNumber 等于495,那么要循環(huán)一遍數(shù)據(jù)庫(kù)花的時(shí)間可就 
長(zhǎng)了。雖然500這個(gè)數(shù)字看起來(lái)大了些,但相比更為穩(wěn)固的企業(yè)解決方案這還是個(gè)小型數(shù)據(jù)庫(kù)了, 
后者通常在一個(gè)數(shù)據(jù)庫(kù)內(nèi)就包含了成千上萬(wàn)條記錄。這時(shí)候不就死定了? 
  采用SQL,你就可以很快地找出準(zhǔn)確的記錄并且打開(kāi)一個(gè)只包含該記錄的recordset,如下所示 
: 
Randomize 
RNumber = Int(Rnd*499) + 1 
  
sql = "Select * FROM Customers Where ID = " & RNumber 
  
set objRec = ObjConn.Execute(SQL) 
Response.WriteRNumber & " = " & objRec("ID") & " " & objRec("c_email") 
  
  不必寫(xiě)出RNumber 和ID,你只需要檢查匹配情況即可。只要你對(duì)以上代碼的工作滿意,你自可 
按需操作“隨機(jī)”記錄。Recordset沒(méi)有包含其他內(nèi)容,因此你很快就能找到你需要的記錄這樣就 
大大降低了處理時(shí)間。 
再談隨機(jī)數(shù) 
  現(xiàn)在你下定決心要榨干Random 函數(shù)的最后一滴油,那么你可能會(huì)一次取出多條隨機(jī)記錄或者 
想采用一定隨機(jī)范圍內(nèi)的記錄。把上面的標(biāo)準(zhǔn)Random 示例擴(kuò)展一下就可以用SQL應(yīng)對(duì)上面兩種情況 
了。 
  為了取出幾條隨機(jī)選擇的記錄并存放在同一recordset內(nèi),你可以存儲(chǔ)三個(gè)隨機(jī)數(shù),然后查詢 
數(shù)據(jù)庫(kù)獲得匹配這些數(shù)字的記錄: 
sql = "Select * FROM Customers Where ID = " & RNumber & " or ID = " & RNumber2 & " or 
ID = " & RNumber3 
  
  假如你想選出10條記錄(也許是每次頁(yè)面裝載時(shí)的10條鏈接的列表),你可以用BETWEEN 或者 
數(shù)學(xué)等式選出第一條記錄和適當(dāng)數(shù)量的遞增記錄。這一操作可以通過(guò)好幾種方式來(lái)完成,但是 
Select 語(yǔ)句只顯示一種可能(這里的ID 是自動(dòng)生成的號(hào)碼): 
sql = "Select * FROM Customers Where ID BETWEEN " & RNumber & " AND " & RNumber & "+ 
9" 
  注意:以上代碼的執(zhí)行目的不是檢查數(shù)據(jù)庫(kù)內(nèi)是否有9條并發(fā)記錄。 
  
隨機(jī)讀取若干條記錄,測(cè)試過(guò) 
Access語(yǔ)法:Select top 10 * From 表名 orDER BY Rnd(id) 
sql server:select top n * from 表名 order by newid() 
mysqlelect * From 表名 order By rand() Limit n 
Access左連接語(yǔ)法(最近開(kāi)發(fā)要用左連接,Access幫助什么都沒(méi)有,網(wǎng)上沒(méi)有Access的SQL說(shuō)明,只有 
自己測(cè)試, 現(xiàn)在記下以備后查) 
語(yǔ)法elect table1.fd1,table1,fd2,table2.fd2 From table1 left join table2 on 
table1.fd1,table2.fd1 where ... 
使用SQL語(yǔ)句 用...代替過(guò)長(zhǎng)的字符串顯示 
語(yǔ)法: 
SQL數(shù)據(jù)庫(kù):select case when len(field)>10 then left(field,10)+'...' else field end as 
news_name,news_id from tablename 
Access數(shù)據(jù)庫(kù):Select iif(len(field)>2,left(field,2)+'...',field) FROM tablename; 
  
Conn.Execute說(shuō)明 
Execute方法 
  該方法用于執(zhí)行SQL語(yǔ)句。根據(jù)SQL語(yǔ)句執(zhí)行后是否返回記錄集,該方法的使用格式分為以下兩 
種: 
(1).執(zhí)行SQL查詢語(yǔ)句時(shí),將返回查詢得到的記錄集。用法為: 
    Set 對(duì)象變量名=連接對(duì)象.Execute("SQL 查詢語(yǔ)言") 
   Execute方法調(diào)用后,會(huì)自動(dòng)創(chuàng)建記錄集對(duì)象,并將查詢結(jié)果存儲(chǔ)在該記錄對(duì)象中,通過(guò)Set 
方法,將記錄集賦給指定的對(duì)象保存,以后對(duì)象變量就代表了該記錄集對(duì)象。 
    (2).執(zhí)行SQL的操作性語(yǔ)言時(shí),沒(méi)有記錄集的返回。此時(shí)用法為: 
    連接對(duì)象.Execute "SQL 操作性語(yǔ)句" [, RecordAffected][, Option] 
      ?RecordAffected 為可選項(xiàng),此出可放置一個(gè)變量,SQL語(yǔ)句執(zhí)行后,所生效的記錄 
數(shù)會(huì)自動(dòng)保存到該變量中。通過(guò)訪問(wèn)該變量,就可知道SQL語(yǔ)句隊(duì)多少條記錄進(jìn)行了操作。 
      ?Option 可選項(xiàng),該參數(shù)的取值通常為adCMDText,它用于告訴ADO,應(yīng)該將Execute 
方法之后的第一個(gè)字符解釋為命令文本。通過(guò)指定該參數(shù),可使執(zhí)行更高效。 
*BeginTrans、RollbackTrans、CommitTrans方法 
  這三個(gè)方法是連接對(duì)象提供的用于事務(wù)處理的方法。BeginTrans用于開(kāi)始一個(gè)事物; 
RollbackTrans用于回滾事務(wù);CommitTrans用于提交所有的事務(wù)處理結(jié)果,即確認(rèn)事務(wù)的處理。 
  事務(wù)處理可以將一組操作視為一個(gè)整體,只有全部語(yǔ)句都成功執(zhí)行后,事務(wù)處理才算成功;若 
其中有一個(gè)語(yǔ)句執(zhí)行失敗,則整個(gè)處理就算失敗,并恢復(fù)到處里前的狀態(tài)。 
  BeginTrans和CommitTrans用于標(biāo)記事務(wù)的開(kāi)始和結(jié)束,在這兩個(gè)之間的語(yǔ)句,就是作為事務(wù) 
處理的語(yǔ)句。判斷事務(wù)處理是否成功,可通過(guò)連接對(duì)象的Error集合來(lái)實(shí)現(xiàn),若Error集合的成員個(gè) 
數(shù)不為0,則說(shuō)明有錯(cuò)誤發(fā)生,事務(wù)處理失敗。Error集合中的每一個(gè)Error對(duì)象,代表一個(gè)錯(cuò)誤信 
息。 
37、一道SQL語(yǔ)句面試題,關(guān)于group by 
表內(nèi)容: 
2005-05-09 勝 
2005-05-09 勝 
2005-05-09 負(fù) 
2005-05-09 負(fù) 
2005-05-10 勝 
2005-05-10 負(fù) 
2005-05-10 負(fù) 
如果要生成下列結(jié)果, 該如何寫(xiě)sql語(yǔ)句? 
勝 負(fù) 
2005-05-09 2 2 
2005-05-10 1 2 
答:代碼如下: 
create table #tmp(rq varchar(10),shengfu nchar(1)) 
insert into #tmp values('2005-05-09','勝') 
insert into #tmp values('2005-05-09','勝') 
insert into #tmp values('2005-05-09','負(fù)') 
insert into #tmp values('2005-05-09','負(fù)') 
insert into #tmp values('2005-05-10','勝') 
insert into #tmp values('2005-05-10','負(fù)') 
insert into #tmp values('2005-05-10','負(fù)') 
1)select rq, sum(case when shengfu='勝' then 1 else 0 end)'勝',sum(case when shengfu=' 
負(fù)' then 1 else 0 end)'負(fù)' from #tmp group by rq 
2) select N.rq,N.勝,M.負(fù) from ( 
select rq,勝=count(*) from #tmp where shengfu='勝'group by rq)N inner join 
(select rq,負(fù)=count(*) from #tmp where shengfu='負(fù)'group by rq)M on N.rq=M.rq 
3)select a.col001,a.a1 勝,b.b1 負(fù) from 
(select col001,count(col001) a1 from temp1 where col002='勝' group by col001) a, 
(select col001,count(col001) b1 from temp1 where col002='負(fù)' group by col001) b 
where a.col001=b.col001 
38、請(qǐng)教一個(gè)面試中遇到的SQL語(yǔ)句的查詢問(wèn)題 
表中有A B C三列,用SQL語(yǔ)句實(shí)現(xiàn):當(dāng)A列大于B列時(shí)選擇A列否則選擇B列,當(dāng)B列大于C列時(shí)選擇B列 
否則選擇C列。 
示例如下: 
select (case when a>b then a else b end ), 
(case when b>c then b esle c end) 
from table_name 
39、一個(gè)日期判斷的sql語(yǔ)句? 
請(qǐng)取出tb_send表中日期(SendTime字段)為當(dāng)天的所有記錄?(SendTime字段為datetime型,包含日 
期與時(shí)間) 
示例如下: 
select * from tb where datediff(dd,SendTime,getdate())=0 
40、有一張表,里面有3個(gè)字段:語(yǔ)文,數(shù)學(xué),英語(yǔ)。其中有3條記錄分別表示語(yǔ)文70分,數(shù)學(xué)80分 
,英語(yǔ)58分,請(qǐng)用一條sql語(yǔ)句查詢出這三條記錄并按以下條件顯示出來(lái)(并寫(xiě)出您的思路): 
大于或等于80表示優(yōu)秀,大于或等于60表示及格,小于60分表示不及格。 
顯示格式: 
語(yǔ)文 數(shù)學(xué) 英語(yǔ) 
及格 優(yōu)秀 不及格 
示例如下: 
select 
(case when 語(yǔ)文>=80 then '優(yōu)秀' 
when 語(yǔ)文>=60 then '及格' 
else '不及格') as 語(yǔ)文, 
(case when 數(shù)學(xué)>=80 then '優(yōu)秀' 
when 數(shù)學(xué)>=60 then '及格' 
else '不及格') as 數(shù)學(xué), 
(case when 英語(yǔ)>=80 then '優(yōu)秀' 
when 英語(yǔ)>=60 then '及格' 
else '不及格') as 英語(yǔ), 
from table 
41、在sqlserver2000中請(qǐng)用sql創(chuàng)建一張用戶臨時(shí)表和系統(tǒng)臨時(shí)表,里面包含兩個(gè)字段ID和 
IDValues,類型都是int型,并解釋下兩者的區(qū)別? 
用戶臨時(shí)表:create table #xx(ID int, IDValues int) 
系統(tǒng)臨時(shí)表:create table ##xx(ID int, IDValues int) 
區(qū)別: 
用戶臨時(shí)表只對(duì)創(chuàng)建這個(gè)表的用戶的Session可見(jiàn),對(duì)其他進(jìn)程是不可見(jiàn)的. 
當(dāng)創(chuàng)建它的進(jìn)程消失時(shí)這個(gè)臨時(shí)表就自動(dòng)刪除. 
全局臨時(shí)表對(duì)整個(gè)SQL Server實(shí)例都可見(jiàn),但是所有訪問(wèn)它的Session都消失的時(shí)候,它也自動(dòng)刪除. 
42、sqlserver2000是一種大型數(shù)據(jù)庫(kù),他的存儲(chǔ)容量只受存儲(chǔ)介質(zhì)的限制,請(qǐng)問(wèn)它是通過(guò)什么方 
式實(shí)現(xiàn)這種無(wú)限容量機(jī)制的。 
它的所有數(shù)據(jù)都存儲(chǔ)在數(shù)據(jù)文件中(*.dbf),所以只要文件夠大,SQL Server的存儲(chǔ)容量是可以擴(kuò) 
大的. 
SQL Server 2000 數(shù)據(jù)庫(kù)有三種類型的文件: 
主要數(shù)據(jù)文件 
主要數(shù)據(jù)文件是數(shù)據(jù)庫(kù)的起點(diǎn),指向數(shù)據(jù)庫(kù)中文件的其它部分。每個(gè)數(shù)據(jù)庫(kù)都有一個(gè)主要數(shù)據(jù)文件 
。主要數(shù)據(jù)文件的推薦文件擴(kuò)展名是 .mdf。 
次要數(shù)據(jù)文件 
次要數(shù)據(jù)文件包含除主要數(shù)據(jù)文件外的所有數(shù)據(jù)文件。有些數(shù)據(jù)庫(kù)可能沒(méi)有次要數(shù)據(jù)文件,而有些 
數(shù)據(jù)庫(kù)則有多個(gè)次要數(shù)據(jù)文件。次要數(shù)據(jù)文件的推薦文件擴(kuò)展名是 .ndf。 
日志文件 
日志文件包含恢復(fù)數(shù)據(jù)庫(kù)所需的所有日志信息。每個(gè)數(shù)據(jù)庫(kù)必須至少有一個(gè)日志文件,但可以不止 
一個(gè)。日志文件的推薦文件擴(kuò)展名是 .ldf。 
43、請(qǐng)用一個(gè)sql語(yǔ)句得出結(jié)果 
從table1,table2中取出如table3所列格式數(shù)據(jù),注意提供的數(shù)據(jù)及結(jié)果不準(zhǔn)確,只是作為一個(gè)格 
式向大家請(qǐng)教。 
如使用存儲(chǔ)過(guò)程也可以。 
table1 
月份mon 部門(mén)dep 業(yè)績(jī)yj 
一月份 01 10 
一月份 02 10 
一月份 03 5 
二月份 02 8 
二月份 04 9 
三月份 03 8 
table2 
部門(mén)dep 部門(mén)名稱dname 
-------------------------------- 
01 國(guó)內(nèi)業(yè)務(wù)一部 
02 國(guó)內(nèi)業(yè)務(wù)二部 
03 國(guó)內(nèi)業(yè)務(wù)三部 
04 國(guó)際業(yè)務(wù)部 
table3 (result) 
部門(mén)dep 一月份 二月份 三月份 
-------------------------------------- 
01 10 null null 
02 10 8 null 
03 null 5 8 
04 null null 9 
------------------------------------------ 
1) 
select a.部門(mén)名稱dname,b.業(yè)績(jī)yj as '一月份',c.業(yè)績(jī)yj as '二月份',d.業(yè)績(jī)yj as '三月份' 
from table1 a,table2 b,table2 c,table2 d 
where a.部門(mén)dep = b.部門(mén)dep and b.月份mon = '一月份' and 
a.部門(mén)dep = c.部門(mén)dep and c.月份mon = '二月份' and 
a.部門(mén)dep = d.部門(mén)dep and d.月份mon = '三月份' and 
2) 
select a.dep, 
sum(case when b.mon=1 then b.yj else 0 end) as '一月份', 
sum(case when b.mon=2 then b.yj else 0 end) as '二月份', 
sum(case when b.mon=3 then b.yj else 0 end) as '三月份', 
sum(case when b.mon=4 then b.yj else 0 end) as '四月份', 
sum(case when b.mon=5 then b.yj else 0 end) as '五月份', 
sum(case when b.mon=6 then b.yj else 0 end) as '六月份', 
sum(case when b.mon=7 then b.yj else 0 end) as '七月份', 
sum(case when b.mon=8 then b.yj else 0 end) as '八月份', 
sum(case when b.mon=9 then b.yj else 0 end) as '九月份', 
sum(case when b.mon=10 then b.yj else 0 end) as '十月份', 
sum(case when b.mon=11 then b.yj else 0 end) as '十一月份', 
sum(case when b.mon=12 then b.yj else 0 end) as '十二月份', 
from table2 a left join table1 b on a.dep=b.dep 
44、華為一道面試題 
一個(gè)表中的Id有多個(gè)記錄,把所有這個(gè)id的記錄查出來(lái),并顯示有多少條記錄。 
------------------------------------------------- 
select id, Count(*) from tb group by id having count(*)>1 
select*from(select count(ID) as count from table group by ID)T where T.count>1