看了些资料,尝试了一些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行结果
查阅资料,这种写法应该没错,可经测试老是不经过 很郁闷,这种方法也不是很经常使用,有待考察。