sql筆記
熟練一下group by的用法
order by
數(shù)據(jù)為漢字時(shí)加N 如where name=N'張三'
null 為"不知道" ,而不是空值 5+NUll 也等于 Null, 等于null是查不出來(lái)的,
要查出null的值,用 is null , 不為空就是 is not null
select Age,count(*) from emloyee group by age //將每個(gè)年齡分組, 并且計(jì)算總數(shù),而且是group by什么,就select的字段必須出現(xiàn)在group by 中,(聚合函數(shù)除外)
聚合函數(shù)不能出現(xiàn)在where語(yǔ)句中, 所以要用having. 但having不能直接代替where, having只能放這組的過(guò)濾信息
select age,count(*) from employee group by age having count(*)>2 這是正確的
如: select age,count(*) from employee group by age having salary>2000 //這是錯(cuò)誤的,having是對(duì)信息分組后的過(guò)濾 having中只能放select中字段的過(guò)濾信息,having放在group by 的后面
從第幾條到第幾條的數(shù)據(jù)
select top 3 * from employee
where Fnumber not in (select top 5 Fnumber from employee order by Fsalary desc)
order by Fsalary desc
sqlserver2005 增加了Row_Number函數(shù)實(shí)現(xiàn) //讀取從第幾條到第幾條的數(shù)據(jù)
模糊查詢
SQL模糊查詢,使用like比較關(guān)鍵字,加上SQL里的通配符,請(qǐng)參考以下:
1、LIKE'Mc%' 將搜索以字母 Mc 開頭的所有字符串(如 McBadden)。
2、LIKE'%inger' 將搜索以字母 inger 結(jié)尾的所有字符串(如 Ringer、Stringer)。
3、LIKE'%en%' 將搜索在任何位置包含字母 en 的所有字符串(如 Bennet、Green、McBadden)。
4、LIKE'_heryl' 將搜索以字母 heryl 結(jié)尾的所有六個(gè)字母的名稱(如 Cheryl、Sheryl)。
5、LIKE'[CK]ars[eo]n' 將搜索下列字符串:Carsen、Karsen、Carson 和 Karson(如 Carson)。
6、LIKE'[M-Z]inger' 將搜索以字符串 inger 結(jié)尾、以從 M 到 Z 的任何單個(gè)字母開頭的所有名稱(如 Ringer)。
7、LIKE'M[^c]%' 將搜索以字母 M 開頭,并且第二個(gè)字母不是 c 的所有名稱(如MacFeather)。
消除重復(fù)數(shù)據(jù)
distinct 只會(huì)消除正行的重復(fù)信息
如 select distinct Fdepartment,Fsubcompany from T_empoyee,中只會(huì)消除 Fdepartment和Fsubcompany 都重復(fù)的信息
聯(lián)合信息表
Union 將兩個(gè)結(jié)果集結(jié)合在一起,select 的字段個(gè)數(shù),類型必須是一致,不過(guò)會(huì)將重復(fù)的數(shù)據(jù)合并掉, 只顯示一條
union all 可以顯示重復(fù)行 // 一般用 union all
常用的方法報(bào)表
顯示臨時(shí)工的和正式工的最高年齡和最低年齡
select '正式工的最高年齡' ,Max(Fage) from T_employee
union
select '正式工的最低年齡' ,Min(Fage) from T_employee
select round(3.1415926,3)
3.1420000
函數(shù)
流控函數(shù)
select Fname,
(
case i //當(dāng)判斷條件的時(shí)候,case不能有值
when 1 then '普通客戶'
when 2 then '會(huì)員'
when 3 then 'vip'
else '未知客戶'
end
) as 客戶類型
from T_Customer
索引
* 全表掃描:對(duì)數(shù)據(jù)進(jìn)行檢索,效率最差的是全表掃描
為經(jīng)常查詢的字段查詢建立索引,缺點(diǎn)降低insert,update,delete的執(zhí)行效率
[picPath] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
Jion
1、select 1 與 select *的區(qū)別
selelct 常量 from ... 對(duì)應(yīng)所有行,返回的永遠(yuǎn)只有一個(gè)值,即常量 。所以正常只會(huì)用來(lái)判斷是否有還是沒(méi)有(比如exists子句)。而select * from ... 是返回所有行的所有列。
性能上的差異,關(guān)鍵看你的from和where子句。比如說(shuō)如果你的where條件中可以通過(guò)索引,那顯然 select 1 from ... 的性能比 select * from ... 好。
2、select count(1)與select count(*)的區(qū)別
跟表結(jié)構(gòu)有關(guān)系:
如果表中沒(méi)有主鍵,那么count(1)比count(*)快
如果有主鍵,那么count(主鍵,聯(lián)合主鍵)比count(*)快
如果表中只有一個(gè)字段,count(*)最快
3、select sum(1)的使用
select count(*)返回所有滿足條件的記錄數(shù),此時(shí)同select sum(1)
但是sum()可以傳任意數(shù)字,負(fù)數(shù)、浮點(diǎn)數(shù)都可以,返回的值是傳入值n*滿足條件記錄數(shù)m
//記得
select caid from news where caid in(select id from category)
select caid from news n where exists(select id from category c where c.id=n.caid)
表A(小表),表B(大表)
select * from A where cc in (select cc from B)
效率低,用到了A表上cc列的索引
select * from B where cc in (select cc from A)
效率高,用到了B表上cc列的索引
select * from A where exists(select cc from B where cc=A.cc)
效率高,用到了B表上cc列的索引。
select * from B where exists(select cc from A where cc=B.cc)
效率低,用到了A表上cc列的索引。
--分頁(yè)要用到的
select top 3 id from news where id not in(select top 3 id from news order by id asc)
select top 3 id from news where not exists(select * from (select top 3 id from news order by id asc) T where T.id=news.id)
select top 3 id from news where not exists(select* from(select top 3 id from news order by id asc) T where T.id=news.id)
浙公網(wǎng)安備 33010602011771號(hào)