Documentation Home
MySQL 8.3 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 40.8Mb
PDF (A4) - 40.9Mb
Man Pages (TGZ) - 294.0Kb
Man Pages (Zip) - 409.0Kb
Info (Gzip) - 4.0Mb
Info (Zip) - 4.0Mb
Excerpts from this Manual

MySQL 8.3 Reference Manual  /  ...  /  Parenthesized Query Expressions

15.2.11 括号查询表达式

parenthesized_query_expression:
    ( query_expression [order_by_clause] [limit_clause] )
      [order_by_clause]
      [limit_clause]
      [into_clause]

query_expression:
    query_block [set_op query_block [set_op query_block ...]]
      [order_by_clause]
      [limit_clause]
      [into_clause]

query_block:
    SELECT ... | TABLE | VALUES

order_by_clause:
    ORDER BY as for SELECT

limit_clause:
    LIMIT as for SELECT

into_clause:
    INTO as for SELECT

set_op:
    UNION | INTERSECT | EXCEPT

MySQL 8.3 支持括号查询表达式,按照前面的语法。最简单的情况下,括号查询表达式包含单个 SELECT 或其他返回结果集的语句,无需后续可选子句:

(SELECT 1);
(SELECT * FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'mysql');

TABLE t;

VALUES ROW(2, 3, 4), ROW(1, -2, 3);

括号查询表达式也可以包含通过一个或多个集合操作链接的查询,例如 UNION,并以任何或所有可选子句结尾:

mysql> (SELECT 1 AS result UNION SELECT 2);
+--------+
| result |
+--------+
|      1 |
|      2 |
+--------+
mysql> (SELECT 1 AS result UNION SELECT 2) LIMIT 1;
+--------+
| result |
+--------+
|      1 |
+--------+
mysql> (SELECT 1 AS result UNION SELECT 2) LIMIT 1 OFFSET 1;
+--------+
| result |
+--------+
|      2 |
+--------+
mysql> (SELECT 1 AS result UNION SELECT 2)
       ORDER BY result DESC LIMIT 1;
+--------+
| result |
+--------+
|      2 |
+--------+
mysql> (SELECT 1 AS result UNION SELECT 2)
       ORDER BY result DESC LIMIT 1 OFFSET 1;
+--------+
| result |
+--------+
|      1 |
+--------+
mysql> (SELECT 1 AS result UNION SELECT 3 UNION SELECT 2)
       ORDER BY result LIMIT 1 OFFSET 1 INTO @var;
mysql> SELECT @var;
+------+
| @var |
+------+
|    2 |
+------+

INTERSECTUNIONEXCEPT 之前执行,因此以下两个语句是等效的:

SELECT a FROM t1 EXCEPT SELECT b FROM t2 INTERSECT SELECT c FROM t3;

SELECT a FROM t1 EXCEPT (SELECT b FROM t2 INTERSECT SELECT c FROM t3);

括号查询表达式也用作查询表达式,因此查询表达式通常由查询块组成,也可以由括号查询表达式组成:

(TABLE t1 ORDER BY a) UNION (TABLE t2 ORDER BY b) ORDER BY z;

查询块可以有尾随的 ORDER BYLIMIT 子句,这些子句在外部集合操作之前应用,包括 ORDER BYLIMIT

您不能在没有括号的情况下拥有一个带有尾随 ORDER BYLIMIT 的查询块,但是括号可以以各种方式强制执行:

  • 强制在每个查询块上执行 LIMIT

    (SELECT 1 LIMIT 1) UNION (VALUES ROW(2) LIMIT 1);
    
    (VALUES ROW(1), ROW(2) LIMIT 2) EXCEPT (SELECT 2 LIMIT 1);
  • 强制在查询块和整个查询表达式上执行 LIMIT

    (SELECT 1 LIMIT 1) UNION (SELECT 2 LIMIT 1) LIMIT 1;
  • 强制在整个查询表达式上执行 LIMIT(无括号):

    VALUES ROW(1), ROW(2) INTERSECT VALUES ROW(2), ROW(1) LIMIT 1;
  • 混合执行:在第一个查询块上执行 LIMIT,并在整个查询表达式上执行:

    (SELECT 1 LIMIT 1) UNION SELECT 2 LIMIT 1;

本节描述的语法受某些限制:

  • 如果在括号内有另一个 INTO 子句,则查询表达式尾随的 INTO 子句不允许。

  • 括号查询表达式中的 ORDER BYLIMIT 按照 SQL 标准处理。

    嵌套括号查询表达式是允许的。支持的最大嵌套级别为 63;这是在解析器执行简化或合并后。

    下面是一个示例语句:

    mysql> (SELECT 'a' UNION SELECT 'b' LIMIT 2) LIMIT 3;
    +---+
    | a |
    +---+
    | a |
    | b |
    +---+
    2 rows in set (0.00 sec)

    您应该注意,在折叠括号表达式体时,MySQL遵循 SQL 标准语义,因此外部的更高限制不能覆盖内部的较低限制。例如,(SELECT ... LIMIT 5) LIMIT 10 最多只能返回五行。