子查询就是指在一个 select 语句中嵌套另一个 select 语句.
any,in,some,all 分别是子查询关键词之一,
any 可以与 =,>,>=,<,<=,<> 结合起来使用, 分别表示等于, 大于, 大于等于, 小于, 小于等于, 不等于其中的任何一个数据.
all 可以与 =,>,>=,<,<=,<> 结合是来使用, 分别表示等于, 大于, 大于等于, 小于, 小于等于, 不等于其中的其中的所有数据.
他们进行子查询的语法如下:
- operand comparison_operator any (subquery);
- operand in (subquery);
- operand coparison_operator some (subquery);
- operand comparison_operator all (subquery);
any,all 关键字必须与一个比较操作符一起使用. any 关键词可以理解为 "对于子查询返回的列中的任一数值, 如果比较结果为 true, 则返回 true".
例如:
select s1 from t1 where s1> any (select s1 from t2);
假设表 t1 中有一行包含 (10),t2 包含 (21,14,6), 则表达式为 true; 如果 t2 包含 (20,10), 或者表 t2 为空表, 则表达式为 false. 如果表 t2 包含 (null,null,null), 则表达式为 unkonwn.
all 的意思是 "对于子查询返回的列中的所有值, 如果比较结果为 true, 则返回 true"
例如:
select s1 from t1 where s1> all(select s1 from t2);
假设表 t1 中有一行包含 (10). 如果表 t2 包含 (-5,0,+5), 则表达式为 true, 因为 10 比 t2 中的查出的所有三个值大. 如果表 t2 包含 (12,6,null,-100), 则表达式为 false, 因为 t2 中有一个值 12 大于 10. 如果表 t2 包含 (0,null,1), 则表达式为 unknown. 如果 t2 为空表, 则结果为 true.
not in 是 "<>all" 的别名, 用法相同.
语句 in 与 "=any" 是相同的.
例如:
- select s1 from t1 where s1 = any (select s1 from t2);
- select s1 from t1 where s1 in (select s1 from t2);
语句 some 是 any 的别名, 用法相同.
例如:
- select s1 from t1 where s1 <> any (select s1 from t2);
- select s1 from t1 where s1 <> some (select s1 from t2);
在上述查询中 some 理解上就容易了 "表 t1 中有部分 s1 与 t2 表中的 s1 不相等", 这种语句用 any 理解就有错了.
来源: http://www.bubuko.com/infodetail-2648532.html