Subquery Errors
There are some new error returns which apply only to subqueries. This section groups them together because reviewing them will help remind you of some points.
-
ERROR 1235 (ER_NOT_SUPPORTED_YET) SQLSTATE = 42000 Message = 'This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery''
This means that
SELECT * FROM t1 WHERE s1 IN (SELECT s2 FROM t2 ORDER BY s1 LIMIT 1)
will not work, but only in some early versions, such as MariaDB.
-
ERROR 1240 (ER_CARDINALITY_COL) SQLSTATE = 21000 Message = 'Operand should contain 1 column(s)'
This error will occur in cases like this:
SELECT (SELECT column1, column2 FROM t2) FROM t1;
It's okay to use a subquery that returns multiple columns, if the purpose is comparison. Leia "Row Subqueries". But in other contexts the subquery must be a scalar operand.
-
ERROR 1241 (ER_SUBSELECT_NO_1_ROW) SQLSTATE = 21000 Message = 'Subquery returns more than 1 row'
This error will occur in cases like this:
SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);
but only when there is more than one row in
t2
. That means this error might occur in code that has been working for years, because somebody happened to make a change which affected the number of rows that the subquery can return. Remember that if the object is to find any number of rows, not just one, then the correct statement would look like this:SELECT * FROM t1 WHERE column1 = ANY (SELECT column1 FROM t2);
-
Error 1093 (ER_UPDATE_TABLE_USED) SQLSTATE = HY000 Message = 'You can't specify target table 'x' for update in FROM clause'
This error will occur in cases like this:
UPDATE t1 SET column2 = (SELECT MAX(column1) FROM t1);
It's okay to use a subquery for assignment within an UPDATE
statement, since subqueries are legal in UPDATE
and in DELETE
statements as well as in SELECT
statements. However, you cannot use the same table, in this case table t1
, for both the subquery's FROM
clause and the update target.
Usually, failure of the subquery causes the entire statement to fail.