看了些資料,嘗試了一些sql的幾種常見查詢方式,有些疑問,待解決html
1 聯合查詢的鏈接方式sql
內聯接,左右聯接(inner join,left /right outer join) 很簡單經常使用的聯合查詢方式。ide
2建立子查詢函數
例1:select * from ceshi as e where e.id in測試
( select ceshiid from ceshi1)htm
例2:select * from ceshi1 as e where exists索引
( select * from ceshi1 e1 where e.id=e1.id)get
3建立複合查詢it
商業需求:a種僱員有兩種休假時間 time1和time2,b種僱員有一種休假時間time2,io
以下查詢返回每一個僱員休假的總時間
select id ,
case type
when 1 then time1+time2
else time2
end as paidhours from ceshi
4聚合函數的查詢
avg 求平均值 count 返回行數 max/min返回最大最小值sum等等
5使用pivot和unpivot運算符格式化數據的查詢
例:select [0],[1]
from
(
select salariedFlag ,vacationHourses from gongzi
) as h
pivot
(
avg(vacationHourses)
for salariedFlag in ([0], [1])
) as pvt
這種函數測試:[0]列老是顯示null 很鬱悶,測試沒獲得想要的結果,正在研究中
6建立使用全文搜索的查詢
相比tsql中=和like 兩種查詢條件運算符來講,contains和freetext 具備更高的查詢效率,
但要啓用全文索引http://www.xueit.com/html/2009-03/26_745_00.html
contains精確匹配 ,freetext 返回全部不精確匹配
如幾十萬條數據裏用like查詢的話可能須要幾分鐘的時間,而用後者卻可能在幾秒鐘內完成查詢結果
7使用tablesample字句限制返回的結果
(1) select * from GG_XTRZ tablesample(10 percent)返回10%的結果
(2)SELECT GNID ,id
FROM GG_XTRZ
TABLESAMPLE (100 ROWS)返回100行結果
查閱資料,這種寫法應該沒錯,可經測試老是不經過 很鬱悶,這種方法也不是很經常使用,有待考察。