parenthesized_query_expression
: (query_expression
[order_by_clause
] [limit_clause
] ) [order_by_clause
] [limit_clause
] [into_clause
]query_expression
:query_block
[UNIONquery_block
[UNIONquery_block
...]] [order_by_clause
] [limit_clause
] [into_clause
]query_block
: SELECT ... (see Section 13.2.10, “SELECT Statement”)order_by_clause
: ORDER BY as for SELECT (see Section 13.2.10, “SELECT Statement”)limit_clause
: LIMIT as for SELECT (see Section 13.2.10, “SELECT Statement”)into_clause
: INTO as for SELECT (see Section 13.2.10, “SELECT Statement”)
MySQL 8.0.22 and higher supports parenthesized query expressions according to the preceding syntax. MySQL8.0.22及更高版本根据前面的语法支持带圆括号的查询表达式。At its simplest, a parenthesized query expression contains a single 最简单的情况是,带圆括号的查询表达式只包含一个SELECT
and no following optional clauses:SELECT
子句,不包含以下可选子句:
(SELECT 1); (SELECT * FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'mysql');
A parenthesized query expression can also contain a 带圆括号的查询表达式还可以包含由多个UNION
comprising multiple SELECT
statements, and end with any or all of the optional clauses:SELECT
语句组成的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 | +------+
Parenthesized query expressions are also used as query expressions, so a query expression, usually composed of query blocks, may also consist of parenthesized query expressions:带圆括号的查询表达式也可用作查询表达式,因此通常由查询块组成的查询表达式也可能由带圆括号的查询表达式组成:
(SELECT * FROM t1 ORDER BY a) UNION (SELECT * FROM t2 ORDER BY b) ORDER BY z;
Query blocks may have trailing 查询块可能有ORDER BY
and LIMIT
clauses, which are applied before the outer UNION
and ORDER BY
and LIMIT
.ORDER BY
和LIMIT
子句,它们应用于外部UNION
和ORDER BY
和LIMIT
之前。
You cannot have a query block with a trailing 如果查询块的尾部带有ORDER BY
or LIMIT
, without wrapping it in parentheses, but parentheses may be used for enforcement in various ways:ORDER BY
或LIMIT
,则不能将其括在圆括号中,但圆括号可用于以各种方式强制执行:
To enforce 要对每个查询块实施LIMIT
on each query block:LIMIT
:
(SELECT 1 LIMIT 1) UNION (SELECT 2 LIMIT 1);
To enforce 要对查询块和整个查询表达式实施LIMIT
on both query blocks and the entire query expression:LIMIT
:
(SELECT 1 LIMIT 1) UNION (SELECT 2 LIMIT 1) LIMIT 1;
To enforce 要对整个查询表达式(不带括号)实施LIMIT
on the entire query expression (with no parentheses):LIMIT
:
SELECT 1 UNION SELECT 2 LIMIT 1;
Hybrid enforcement: 混合强制:第一个查询块上的LIMIT
on the first query block and on the entire query expression:LIMIT
和整个查询表达式上的LIMIT
:
(SELECT 1 LIMIT 1) UNION SELECT 2 LIMIT 1;
The syntax described in this section is subject to certain restrictions:本节中描述的语法受某些限制:
If 如果ORDER BY
occurs within a parenthesized query expression and also is applied in the outer query, the results are undefined and may change in a future version of MySQL. ORDER BY
出现在带圆括号的查询表达式中,并且也应用于外部查询中,则结果是未定义的,可能会在MySQL的未来版本中更改。The same is true if 如果LIMIT
occurs within a parenthesized query expression and also is applied in the outer query.LIMIT
出现在带圆括号的查询表达式中,并且也应用在外部查询中,则情况也是如此。
A trailing 如果括号内有另一个INTO
clause for a query expression is not permitted if there is another INTO
clause inside parentheses.INTO
子句,则不允许查询表达式的尾随INTO
子句。
Parenthesized query expressions do not permit multiple levels of 带圆括号的查询表达式不允许多级ORDER BY
or LIMIT
operations. ORDER BY
或LIMIT
操作。For example:例如:
mysql> (SELECT 'a' UNION SELECT 'b' LIMIT 1) LIMIT 2;
ERROR 1235 (42000): This version of MySQL doesn't yet support 'parenthesized
query expression with more than one external level of ORDER/LIMIT operations'