Syntax:语法:
operandcomparison_operatorANY (subquery)operandIN (subquery)operandcomparison_operatorSOME (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小于10。The expression is 如果表FALSE if table t2 contains (20,10), or if table t2 is empty. t2包含(20,10),或者表t2为空,则表达式为FALSE。The expression is unknown (that is, 如果表t2包含(NULL) if table t2 contains (NULL,NULL,NULL).NULL,NULL,NULL),则表达式是未知的(即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. SOME是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 <> 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 从MySQL8.0.19开始,只要表只包含一列,就可以在标量TABLE in a scalar IN, ANY, or SOME subquery provided the table contains only a single column. IN、ANY或SOME子查询中使用TABLE。If 如果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);