Syntax:语法
operandcomparison_operatorALL (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的值12。The 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为空,则表达式为TRUE。So, 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 MySQL 8.0.19支持TABLE statement. TABLE语句。As with 就像用IN, ANY, and SOME, you can use TABLE with ALL and NOT IN provided that the following two conditions are met:IN、ANY和SOME那样,只要满足下面的两个条件,你就可以使用TABLE配合ALL和NOT IN:
The table in the subquery contains only one column子查询中的表只包含一列
The subquery does not depend on a column expression子查询不依赖于列表达式
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,因为子查询依赖于表表达式。