13.2.11.4 Subqueries with ALL带有ALL的子查询

Syntax:语法

operand comparison_operator ALL (subquery)

The word ALL, which must follow a comparison operator, means return TRUE if the comparison is TRUE for ALL of the values in the column that the subquery returns. 单词ALL必须跟在比较运算符后面,意思是“如果对子查询返回的列中的所有值的比较都为TRUE,则返回TRUE”。For example:例如:

SELECT s1 FROM t1 WHERE s1 > ALL (SELECT s1 FROM t2);

Suppose that there is a row in table t1 containing (10). 假设表t1中有一行包含(10)The expression is TRUE if table t2 contains (-5,0,+5) because 10 is greater than all three values in t2. 如果表t2包含(-5,0,+5),则表达式为TRUE,因为10大于t2中的所有三个值。The expression is FALSE if table t2 contains (12,6,NULL,-100) because there is a single value 12 in table t2 that is greater than 10. 如果表t2包含(12,6,NULL,-100),则表达式为FALSE,因为表t2中有一个大于10的值12The expression is unknown (that is, NULL) if table t2 contains (0,NULL,1).如果表t2包含(0,NULL,1),则表达式未知(即NULL)。

Finally, the expression is TRUE if table t2 is empty. 最后,如果表t2为空,则表达式为TRUESo, the following expression is TRUE when table t2 is empty:因此,当表t2为空时,以下表达式为真:

SELECT * FROM t1 WHERE 1 > ALL (SELECT s1 FROM t2);

But this expression is NULL when table t2 is empty:但表t2为空时,此表达式为NULL

SELECT * FROM t1 WHERE 1 > (SELECT s1 FROM t2);

In addition, the following expression is NULL when table t2 is empty:此外,当表t2为空时,以下表达式为NULL

SELECT * FROM t1 WHERE 1 > ALL (SELECT MAX(s1) FROM t2);

In general, tables containing NULL values and empty tables are edge cases. 通常,包含空值和空表的表是“边缘情况”。When writing subqueries, always consider whether you have taken those two possibilities into account.在编写子查询时,请始终考虑是否已将这两种可能性考虑在内。

NOT IN is an alias for <> ALL. NOT IN<> ALL的别名。Thus, these two statements are the same:因此,这两种说法是相同的:

SELECT s1 FROM t1 WHERE s1 <> ALL (SELECT s1 FROM t2);
SELECT s1 FROM t1 WHERE s1 NOT IN (SELECT s1 FROM t2);

MySQL 8.0.19 supports the TABLE statement. MySQL 8.0.19支持TABLE语句。As with IN, ANY, and SOME, you can use TABLE with ALL and NOT IN provided that the following two conditions are met:就像用INANYSOME那样,只要满足下面的两个条件,你就可以使用TABLE配合ALLNOT IN

For example, assuming that table t2 consists of a single column, the last two statements shown previously can be written using TABLE t2 like this:例如,假设表t2由一列组成,前面显示的最后两个语句可以使用TABLE t2编写,如下所示:

SELECT s1 FROM t1 WHERE s1 <> ALL (TABLE t2);
SELECT s1 FROM t1 WHERE s1 NOT IN (TABLE t2);

A query such as SELECT * FROM t1 WHERE 1 > ALL (SELECT MAX(s1) FROM t2); cannot be written using TABLE t2 because the subquery depends on a column expression.诸如SELECT * FROM t1 WHERE 1 > ALL (SELECT MAX(s1) FROM t2);这样的查询不能被写成使用TABLE t2,因为子查询依赖于表表达式。