SQL Server 中的 ALL、SOME 和ANY
作者:數(shù)據(jù)超酷
本篇給大家介紹SQL Server 中的 ALL、SOME 和ANY三個比較運算符,其中 SOME 和 ANY 是等效的。
ALL、SOME 和 ANY是SQL Server 三個比較運算符,其中 SOME 和 ANY 是等效的
SOME 或 ANY:當(dāng)表達(dá)式與子查詢返回的至少一個值比較時滿足比較條件, SOME 或 ANY 就返回 TRUE。否則返回 FALSE
ALL :當(dāng)表達(dá)式與子查詢返回的所有值比較時滿足比較條件, ALL 就返回 TRUE。否則返回 FALSE
- create table test01 ( num int)
- insert into test01
- select 5 union
- select 6
- create table test02 (num int)
- insert into test02
- select 2 union
- select 3 union
- select 6 union
- select 7
- select * from test01
- select * from test02
至此 test01 表中有數(shù)據(jù): 5和 6 , test02表中有數(shù)據(jù) :2,3,6,7
- select num as num1 from test02 where num > all(select num from test01 )
- select num as num2 from test02 where num > any(select num from test01 )
- select num as num3 from test02 where num > some(select num from test01)
- select num as num4 from test02 where num = all (select num from test01 )
- select num as num5 from test02 where num = any(select num from test01 )
- select num as num6 from test02 where num = some(select num from test01)
- select num as num7 from test02 where num <> all (select num from test01 )
- select num as num8 from test02 where num <> any (select num from test01 )
- select num as num9 from test02 where num <>some(select num from test01)
從上面的測試結(jié)果看,= any 或 = some 與平時使用的 in 是等效的。
責(zé)任編輯:姜華
來源:
今日頭條