13.2.11.3 Subqueries with ANY, IN, or SOME包含ANY、IN或SOME的子查询

Syntax:语法:

operand comparison_operator ANY (subquery)
operand IN (subquery)
operand comparison_operator SOME (subquery)

Where comparison_operator is one of these operators:其中comparison_operator是下列运算符之一:

=  >  <  >=  <=  <>  !=

The ANY keyword, which must follow a comparison operator, means return TRUE if the comparison is TRUE for ANY of the values in the column that the subquery returns. ANY关键字必须跟在比较运算符后面,表示“如果子查询返回的列中的任何值的比较为TRUE,则返回TRUE。”For example:例如:

SELECT s1 FROM t1 WHERE s1 > ANY (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 (21,14,7) because there is a value 7 in t2 that is less than 10. 如果表t2包含(21,14,7),则表达式为真,因为t2中的值7小于10The expression is FALSE if table t2 contains (20,10), or if table t2 is empty. 如果表t2包含(20,10),或者表t2为空,则表达式为FALSEThe expression is unknown (that is, NULL) if table t2 contains (NULL,NULL,NULL).如果表t2包含(NULLNULLNULL),则表达式是未知的(即NULL)。

When used with a subquery, the word IN is an alias for = ANY. 与子查询一起使用时,IN=ANY的别名。Thus, these two statements are the same:因此,这两种说法是相同的:

SELECT s1 FROM t1 WHERE s1 = ANY (SELECT s1 FROM t2);
SELECT s1 FROM t1 WHERE s1 IN    (SELECT s1 FROM t2);

IN and = ANY are not synonyms when used with an expression list. 与表达式列表一起使用时,IN=ANY不是同义词。IN can take an expression list, but = ANY cannot. IN可以接受表达式列表,但是=ANY不能。See Section 12.4.2, “Comparison Functions and Operators”.参见第12.4.2节,“比较函数和运算符”

NOT IN is not an alias for <> ANY, but for <> ALL. NOT IN不是<> ANY的别名,但是它是<> ALL的别名。See Section 13.2.11.4, “Subqueries with ALL”.第13.2.11.4节,“带ALL的子查询”

The word SOME is an alias for ANY. 单词SOMEANY的别名。Thus, these two statements are the same:因此,这两种说法是相同的:

SELECT s1 FROM t1 WHERE s1 <> ANY  (SELECT s1 FROM t2);
SELECT s1 FROM t1 WHERE s1 <> SOME (SELECT s1 FROM t2);

Use of the word SOME is rare, but this example shows why it might be useful. SOME这个词很少使用,但这个例子说明了为什么它可能有用。To most people, the English phrase a is not equal to any b means there is no b which is equal to a, but that is not what is meant by the SQL syntax. 对大多数人来说,英语短语“a不等于任何b”的意思是“没有b等于a”,但这不是SQL语法的意思。The syntax means there is some b to which a is not equal. 语法的意思是“有一些b与a不相等”。Using <> SOME instead helps ensure that everyone understands the true meaning of the query.改用<> SOME有助于确保每个人都理解查询的真正含义。

Beginning with MySQL 8.0.19, you can use TABLE in a scalar IN, ANY, or SOME subquery provided the table contains only a single column. 从MySQL8.0.19开始,只要表只包含一列,就可以在标量INANYSOME子查询中使用TABLEIf t2 has only one column, the statements shown previously in this section can be written as shown here, in each case substituting TABLE t2 for SELECT s1 FROM t2:如果t2只有一列,则本节前面所示的语句可以如下所示编写,在每种情况下,用表t2代替SELECT s1 FROM t2

SELECT s1 FROM t1 WHERE s1 > ANY (TABLE t2);

SELECT s1 FROM t1 WHERE s1 = ANY (TABLE t2);

SELECT s1 FROM t1 WHERE s1 IN (TABLE t2);

SELECT s1 FROM t1 WHERE s1 <> ANY  (TABLE t2);

SELECT s1 FROM t1 WHERE s1 <> SOME (TABLE t2);