被遺忘的SQLServer比較運(yùn)算符謂詞
發(fā)布日期:2022-01-31 16:59 | 文章來(lái)源:源碼之家
http://technet.microsoft.com/zh-cn/library/ms187074%28SQL.90%29.aspx
他們作用于比較運(yùn)算符和子查詢之間,作用類(lèi)似Exists、not exists、in、not in以及其他邏輯意義,這些語(yǔ)法同樣被SQLServer2000支持但是很少看到有人用它們。
復(fù)制代碼 代碼如下:
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 ) --無(wú)數(shù)據(jù)
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數(shù)據(jù),那么所有All相關(guān)的比較運(yùn)算將不會(huì)返回任何結(jié)果,原因就不用多解釋了。而因?yàn)閠1和t2表的null的存在他們和not exists之類(lèi)的比較符會(huì)有一些區(qū)別。
比如下面兩句
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)
他們邏輯上意義很像但是對(duì)于null的處理卻是恰恰相反,第一句會(huì)忽略子查詢的null而把t2的null同時(shí)查出來(lái),第二句卻是忽略了t2的null同時(shí)會(huì)因?yàn)閠1中的null而無(wú)法查詢到數(shù)據(jù)。
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章