There are some errors that apply only to subqueries. 有些错误只适用于子查询。This section describes them.本节将对其进行描述。
Unsupported subquery syntax:不支持的子查询语法:
ERROR 1235 (ER_NOT_SUPPORTED_YET) SQLSTATE = 42000 Message = "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'"
This means that MySQL does not support statements like the following:这意味着MySQL不支持以下语句:
SELECT * FROM t1 WHERE s1 IN (SELECT s2 FROM t2 ORDER BY s1 LIMIT 1)
Incorrect number of columns from subquery:子查询的列数不正确:
ERROR 1241 (ER_OPERAND_COL) SQLSTATE = 21000 Message = "Operand should contain 1 column(s)"
This error occurs in cases like this:在以下情况下会发生此错误:
SELECT (SELECT column1, column2 FROM t2) FROM t1;
You may use a subquery that returns multiple columns, if the purpose is row comparison. 如果用于行比较,则可以使用返回多列的子查询。In other contexts, the subquery must be a scalar operand. 在其他上下文中,子查询必须是标量操作数。See Section 13.2.11.5, “Row Subqueries”.请参阅第13.2.11.5节,“行子查询”。
Incorrect number of rows from subquery:子查询中的行数不正确:
ERROR 1242 (ER_SUBSELECT_NO_1_ROW) SQLSTATE = 21000 Message = "Subquery returns more than 1 row"
This error occurs for statements where the subquery must return at most one row but returns multiple rows. 对于子查询最多只能返回一行但返回多行的语句,会发生此错误。Consider the following example:考虑以下示例:
SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);
If 如果SELECT column1 FROM t2
returns just one row, the previous query works. SELECT column1 FROM t2
只返回一行,则上一个查询行得通。If the subquery returns more than one row, error 1242 occurs. 如果子查询返回多行,则发生错误1242。In that case, the query should be rewritten as:在这种情况下,查询应重写为:
SELECT * FROM t1 WHERE column1 = ANY (SELECT column1 FROM t2);
Incorrectly used table in subquery:子查询中未正确使用表:
Error 1093 (ER_UPDATE_TABLE_USED) SQLSTATE = HY000 Message = "You can't specify target table 'x' for update in FROM clause"
This error occurs in cases such as the following, which attempts to modify a table and select from the same table in the subquery:在以下情况下会发生此错误,即试图修改表并从子查询中的同一表中进行选择:
UPDATE t1 SET column2 = (SELECT MAX(column1) FROM t1);
You can use a common table expression or derived table to work around this. 可以使用公共表表达式或派生表来解决此问题。See Section 13.2.11.12, “Restrictions on Subqueries”.请参阅第13.2.11.12节,“子查询限制”。
In MySQL 8.0.19 and later, all of the errors described in this section also apply when using 在MySQL 8.0.19及更高版本中,在子查询中使用TABLE
in subqueries.TABLE
时,本节中描述的所有错误也适用。
For transactional storage engines, the failure of a subquery causes the entire statement to fail. 对于事务性存储引擎,子查询失败会导致整个语句失败。For nontransactional storage engines, data modifications made before the error was encountered are preserved.对于非事务存储引擎,将保留在遇到错误之前所做的数据修改。