zl程序教程

您现在的位置是:首页 >  其他

当前栏目

SQL Server 中的 ALL、SOME 和ANY

2023-03-14 09:41:31 时间

 ALL、SOME 和 ANY是SQL Server 三个比较运算符,其中 SOME 和 ANY 是等效的

SOME 或 ANY:当表达式与子查询返回的至少一个值比较时满足比较条件, SOME 或 ANY 就返回 TRUE。否则返回 FALSE

ALL :当表达式与子查询返回的所有值比较时满足比较条件, ALL 就返回 TRUE。否则返回 FALSE

  1. create table test01 ( num int
  2.  
  3. insert into test01 
  4. select 5 union 
  5. select 6 
  6.  
  7. create table test02 (num int
  8.  
  9. insert into test02 
  10. select 2 union 
  11. select 3 union 
  12. select 6 union 
  13. select 7 
  14.  
  15. select * from test01 

  1. select * from test02 

 

至此 test01 表中有数据: 5和 6 , test02表中有数据 :2,3,6,7

  1. select num as num1 from test02 where num > all(select num from test01 ) 
  2. select num as num2 from test02 where num > any(select num from test01 ) 
  3. select num as num3 from test02 where num > some(select num from test01) 

 

  1. select num as num4 from test02 where num = all (select num from test01 ) 
  2. select num as num5 from test02 where num = any(select num from test01 ) 
  3. select num as num6 from test02 where num = some(select num from test01) 

 

  1. select num as num7 from test02 where num <> all (select num from test01 ) 
  2. select num as num8 from test02 where num <> any (select num from test01 ) 
  3. select num as num9 from test02 where num <>some(select num from test01) 

 

从上面的测试结果看,= any 或 = some 与平时使用的 in 是等效的。