13.2.11 Subqueries子查询

13.2.11.1 The Subquery as Scalar Operand作为标量操作数的子查询
13.2.11.2 Comparisons Using Subqueries使用子查询进行比较
13.2.11.3 Subqueries with ANY, IN, or SOME带有ANY、IN或SOME的子查询
13.2.11.4 Subqueries with ALL带有ALL的子查询
13.2.11.5 Row Subqueries行子查询
13.2.11.6 Subqueries with EXISTS or NOT EXISTS具有EXISTS或NOT EXISTS的子查询
13.2.11.7 Correlated Subqueries关联子查询
13.2.11.8 Derived Tables派生表
13.2.11.9 Lateral Derived Tables横向派生表
13.2.11.10 Subquery Errors子查询错误
13.2.11.11 Optimizing Subqueries优化子查询
13.2.11.12 Restrictions on Subqueries子查询限制

A subquery is a SELECT statement within another statement.子查询是另一条语句中的SELECT语句。

All subquery forms and operations that the SQL standard requires are supported, as well as a few features that are MySQL-specific.支持SQL标准要求的所有子查询表单和操作,以及一些特定于MySQL的特性。

Here is an example of a subquery:下面是子查询的示例:

SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);

In this example, SELECT * FROM t1 ... is the outer query (or outer statement), and (SELECT column1 FROM t2) is the subquery. 在此示例中,SELECT * FROM t1 ...外部查询(或外部语句),而(SELECT column1 FROM t2)子查询We say that the subquery is nested within the outer query, and in fact it is possible to nest subqueries within other subqueries, to a considerable depth. 我们说子查询嵌套在外部查询中,事实上,可以在其他子查询中嵌套子查询,嵌套深度相当大。A subquery must always appear within parentheses.子查询必须始终出现在括号内。

The main advantages of subqueries are:子查询的主要优点是:

Here is an example statement that shows the major points about subquery syntax as specified by the SQL standard and supported in MySQL:下面是一个示例语句,它显示了SQL标准指定的、MySQL支持的子查询语法的要点:

DELETE FROM t1
WHERE s11 > ANY
 (SELECT COUNT(*) /* no hint */ FROM t2
  WHERE NOT EXISTS
   (SELECT * FROM t3
    WHERE ROW(5*t2.s1,77)=
     (SELECT 50,11*s1 FROM t4 UNION SELECT 50,77 FROM
      (SELECT * FROM t5) AS t5)));

A subquery can return a scalar (a single value), a single row, a single column, or a table (one or more rows of one or more columns). 子查询可以返回标量(单个值)、单行、单列或表(一列或多列的一行或多行)。These are called scalar, column, row, and table subqueries. 这些子查询称为标量子查询、列子查询、行子查询和表子查询。Subqueries that return a particular kind of result often can be used only in certain contexts, as described in the following sections.返回特定类型结果的子查询通常只能在某些上下文中使用,如以下部分所述。

There are few restrictions on the type of statements in which subqueries can be used. 对于可以使用子查询的语句类型几乎没有限制。A subquery can contain many of the keywords or clauses that an ordinary SELECT can contain: DISTINCT, GROUP BY, ORDER BY, LIMIT, joins, index hints, UNION constructs, comments, functions, and so on.子查询可以包含普通SELECT可以包含的许多关键字或子句:DISTINCTGROUP BYORDER BYLIMITJOIN、索引提示、UNION构造、注释、函数等等。

Beginning with MySQL 8.0.19, TABLE and VALUES statements can be used in subqueries. 从MySQL 8.0.19开始,TABLEVALUES语句可以用于子查询。Subqueries using VALUES are generally more verbose versions of subqueries that can be rewritten more compactly using set notation, or with SELECT or TABLE syntax; assuming that table ts is created using the statement CREATE TABLE ts VALUES ROW(2), ROW(4), ROW(6), the statements shown here are all equivalent:使用VALUES的子查询通常是更详细的子查询版本,可以使用集合表示法或利用SELECTTABLE语法更简洁地重写;假设表ts是使用CREATE TABLE ts VALUES ROW(2), ROW(4), ROW(6)语句创建的,这里显示的语句都是等效的:

SELECT * FROM tt
    WHERE b > ANY (VALUES ROW(2), ROW(4), ROW(6));

SELECT * FROM tt
    WHERE b > ANY (2, 4, 6);

SELECT * FROM tt
    WHERE b > ANY (SELECT * FROM ts);

SELECT * FROM tt
    WHERE b > ANY (TABLE ts);

Examples of TABLE subqueries are shown in the sections that follow.TABLE子查询的示例显示在下面的部分中。

A subquery's outer statement can be any one of: SELECT, INSERT, UPDATE, DELETE, SET, or DO.子查询的外部语句可以是以下任一语句:SELECTINSERTUPDATEDELETESETDO

For information about how the optimizer handles subqueries, see Section 8.2.2, “Optimizing Subqueries, Derived Tables, View References, and Common Table Expressions”. 有关优化器如何处理子查询的信息,请参阅第8.2.2节,“优化子查询、派生表、视图引用和公共表表达式”For a discussion of restrictions on subquery use, including performance issues for certain forms of subquery syntax, see Section 13.2.11.12, “Restrictions on Subqueries”.有关子查询使用限制的讨论,包括某些形式的子查询语法的性能问题,请参阅第13.2.11.12节,“子查询限制”

13.2.11.1 The Subquery as Scalar Operand
13.2.11.2 Comparisons Using Subqueries
13.2.11.3 Subqueries with ANY, IN, or SOME
13.2.11.4 Subqueries with ALL
13.2.11.5 Row Subqueries
13.2.11.6 Subqueries with EXISTS or NOT EXISTS
13.2.11.7 Correlated Subqueries
13.2.11.8 Derived Tables
13.2.11.9 Lateral Derived Tables
13.2.11.10 Subquery Errors
13.2.11.11 Optimizing Subqueries
13.2.11.12 Restrictions on Subqueries