Subqueries with ALL
语法描述
关键词 ALL 必须跟在比较操作符后面,指如果子查询返回的列中值的 ALL 的比较是 TRUE,则返回 TRUE。
1. operand comparison_operator ALL (subquery)
示例如下:
1. 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 中有一个大于 10 的值 12。如果表 t2 包含 (0,NULL,1),则表达式为 NULL。
- 如果表 t2 为空,则表达式为
TRUE。例如,当下表 t2 为空时,表达式是TRUE:
1. SELECT * FROM t1 WHERE 1 > ALL (SELECT s1 FROM t2);
- 下面示例中,当表 t2 为空时,这个表达式是
NULL:
1. SELECT * FROM t1 WHERE 1 > (SELECT s1 FROM t2);
或:
1. SELECT * FROM t1 WHERE 1 > ALL (SELECT MAX(s1) FROM t2);
说明:在书写子查询语法时,要注意考虑到含有 NULL 值的表和空表的情况。
语法结构
1. > SELECT column_name(s) FROM table_name {WHERE | HAVING} [not] expression comparison_operator ALL (subquery)
示例
1. create table t1 (a int);
2. create table t2 (a int, b int);
3. create table t3 (a int);
4. create table t4 (a int not null, b int not null);
5. create table t5 (a int);
6. create table t6 (a int, b int);
7. insert into t1 values (2);
8. insert into t2 values (1,7),(2,7);
9. insert into t4 values (4,8),(3,8),(5,9);
10. insert into t5 values (null);
11. insert into t3 values (6),(7),(3);
12. insert into t6 values (10,7),(null,7);
14. mysql> select * from t3 where a <> all (select b from t2);
15. +------+
16. | a |
17. +------+
18. | 6 |
19. | 3 |
20. +------+
21. 2 rows in set (0.00 sec)
23. mysql> select * from t4 where 5 > all (select a from t5);
24. +------+------+
25. | a | b |
26. +------+------+
27. | 4 | 8 |
28. | 3 | 8 |
29. | 5 | 9 |
30. +------+------+
31. 3 rows in set (0.01 sec)
33. mysql> select * from t3 where 10 > all (select b from t2);
34. +------+
35. | a |
36. +------+
37. | 6 |
38. | 7 |
39. | 3 |
40. +------+
41. 3 rows in set (0.00 sec)
43. mysql> select a,b from t6 where a > all ( select a ,b from t4 where a>3);
44. ERROR 1105 (HY000): subquery should return 1 column
