被遗忘的SQLServer比较运算符谓词

jinjazz 2009-08-26 04:57:24
加精
SQLServer中有三个关键字可以修改比较运算符:All、Any和Some,其中Some和Any等价。

官方的参考文档

http://technet.microsoft.com/zh-cn/library/ms187074%28SQL.90%29.aspx

他们作用于比较运算符和子查询之间,作用类似Exists、not exists、in、not in以及其他逻辑意义,这些语法同样被SQLServer2000支持但是很少看到有人用它们。

set  nocount  on

use tempdb
go

if (object_id ('t1' ) is not null ) drop table t1
create table t1 (n int )
insert into t1 select 2 union select 3

if (object_id ('t2' ) is not null ) drop table t2
create table t2 (n int )
insert into t2 select 1 union select 2 union select 3 union select 4

select * from t2 where n> all (select n from t1 ) --4
select * from t2 where n> any (select n from t1 ) --3,4
--select * from t2 where n>some(select n from t1) --3,4

select * from t2 where n= all (select n from t1 ) --无数据
select * from t2 where n= any (select n from t1 ) --2,3
--select * from t2 where n=some(select n from t1) --2,3

select * from t2 where n< all (select n from t1 ) --1
select * from t2 where n< any (select n from t1 ) --1,2
--select * from t2 where n<some(select n from t1) --1,2

select * from t2 where n<> all (select n from t1 ) --1,4
select * from t2 where n<> any (select n from t1 ) --1,2,3,4
--select * from t2 where n<>some(select n from t1)--1,2,3,4


set nocount off


注意,如果t1中包含null数据,那么所有All相关的比较运算将不会返回任何结果,原因就不用多解释了。而因为t1和t2表的null的存在他们和not exists之类的比较符会有一些区别。

比如下面两句

select * from t2 a where not exists(select 1 from t1 where n>=a.n)

select * from t2 where n >all(select n from t1)

他们逻辑上意义很像但是对于null的处理却是恰恰相反,第一句会忽略子查询的null而把t2的null同时查出来,第二句却是忽略了t2的null同时会因为t1中的null而无法查询到数据。
...全文
2348 143 打赏 收藏 转发到动态 举报
写回复
用AI写文章
143 条回复
切换为时间正序
请发表友善的回复…
发表回复
jjoulejcc 2009-10-29
  • 打赏
  • 举报
回复
学习
ZHANGJUNPING 2009-08-31
  • 打赏
  • 举报
回复
太太太太太太太太太
ZHANGJUNPING 2009-08-31
  • 打赏
  • 举报
回复
gggggggggggggg
zaq111111 2009-08-31
  • 打赏
  • 举报
回复
这几个关键词效率如何?
7761098 2009-08-30
  • 打赏
  • 举报
回复
可以了解多一点,但是感觉不用太专注这方面,效率有点低
shanling5460 2009-08-30
  • 打赏
  • 举报
回复
人好多啊,我也来凑凑热闹。exists的相关子查询它不用返回任何结果集所以速度比较快。all,[not]in,some都不会使用的,一个exists就可以全部搞定他们了。而且exists可以实现all,[not]in,some不能实现的功能。
呵呵。。。
DengXingJie 2009-08-30
  • 打赏
  • 举报
回复
好贴,收藏
ewang11 2009-08-29
  • 打赏
  • 举报
回复
学习...
itisonlylove_yy 2009-08-29
  • 打赏
  • 举报
回复
thanks
yeah86 2009-08-29
  • 打赏
  • 举报
回复
fuxiaoyang13 2009-08-29
  • 打赏
  • 举报
回复
学习了!!
qiqi860819 2009-08-29
  • 打赏
  • 举报
回复
学习了
尐孑 2009-08-29
  • 打赏
  • 举报
回复
select count(1) from t1
--65536

select count(1) from t2
-65536

CREATE INDEX COL_T1_N ON T1(N)
CREATE INDEX COL_T2_N ON T2(N)

select * from t2 where n> all (select n from t1 ) --4
--1000-1500ms
SELECT * FROM T2 WHERE NOT EXISTS(SELECT 1 FROM T1 WHERE T1.N>=T2.N)
--0-300ms
select * from t2 where n> any (select n from t1 ) --3,4
--400-700ms
SELECT * FROM T2 WHERE EXISTS(SELECT 1 FROM T1 WHERE T2.N>T1.N)
--0-400ms
要你命三千V 2009-08-29
  • 打赏
  • 举报
回复
顶楼主
gw6328 2009-08-29
  • 打赏
  • 举报
回复
好人啊,学习

我觉得select * from t2 a where not exists(select 1 from t1 where n>=a.n)
更绕脑子-_-|||
aloneone 2009-08-29
  • 打赏
  • 举报
回复
受教,收藏
ProjectIdea 2009-08-29
  • 打赏
  • 举报
回复
恭喜发财。
wenboliang 2009-08-29
  • 打赏
  • 举报
回复
每天回帖即可获得10分可用分!
victorcai2006 2009-08-29
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 josy 的回复:]
上学的时候书本说的蛮多,出来工作以后发现几乎没人使用...
[/Quote]

现在大都如此~~~学习收藏了~~MARK
yijianboy 2009-08-28
  • 打赏
  • 举报
回复
mark
加载更多回复(121)

11,848

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 非技术版
社区管理员
  • 非技术版社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧