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 does not yet support 'LIMIT & IN/ALL/ANY/SOME subquery'"
This means that statements of the following form do not work yet:
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 comparison. In other contexts, the subquery must be a scalar operand. See Section 12.2.10.5, “Row Subqueries”.
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 returns more than one row. 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 will work. If the subquery
returns more than one row, error 1242 will occur. 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:
UPDATE t1 SET column2 = (SELECT MAX(column1) FROM t1);
You can use a subquery for assignment within an
UPDATE
statement because
subqueries are legal in
UPDATE
and
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.
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.
User Comments
Wanting to copy a longtext field from one record to another, I first tried:
Update table set list=(select list from t1 where recno=230) where recno=169
I expected this to select one value from record 230 and copy it into record 169. Instead it fails with Error 1093. Even though this is a single scalar value, MySQL will not let you use the same table in both the update and from parts.
I got the desired result using a temp variable and two queries:
Set @Guy = (select list from t1 where recno=230);
Update t1 set list=@Guy where recno=169
Note that the semicolon separates the two statements (in phpMyAdmin). Since the temp variable is connection specific, the two queries must be run together.
There is a workaround for the use of LIMIT in a subquery, just use a variable (seperate query, execute this one first):
SET @i = 0;
And then the select-query with the subquery including the LIMIT:
SELECT
*
FROM
my_table
WHERE
id_my_other_table IN(
SELECT id FROM my_other_table
WHERE
( @i := ( @i +1 ) ) <= 10
);
In this case there's a limit of 10 results in the subquery.
Tested in version 5.0.45
For a workaround with on the update query look at
When I try to run update query for my table "comments", MySQL returns the #1093 - You can't specify target table 'comments' for update in FROM clause message. My contrived table structure and update query are as follow:
CREATE TABLE comments(id int primary key, phrase text, uid int);
INSERT INTO comments VALUES(1, 'admin user comments',1), (2, 'HR User Comments',2), (3, 'RH User Comments',2);
UPDATE comments SET phrase =
(SELECT phrase FROM comments WHERE uid=2 AND id=2)
WHERE id = 3;
Is there any easy way to work around the #1093 - You can't specify target table 'comments' for update in FROM clause error?
Answer No: 156
Actually, your above update query seems illegal as per SQL standard. MySQL does not allow to UPDATE or DELETE a table's data if you're simultaneously reading that same data with a subquery. Because you are doing so that is why MySQL tersely said its such error message. Therefore, you will have to rewrite your above update query.
Since MySQL materializes sub queries in the FROM Clause as temporary tables, wrapping the subquery into another inner subquery in the FROM Clause causes it to be executed and stored into a temporary table, then referenced implicitly in the outer subquery. So, the update query will succeed by rewriting it like below:
UPDATE comments SET phrase =
( SELECT phrase FROM ( SELECT * FROM comments) AS c1 WHERE c1.uid=2 AND c1.id=2 ) WHERE id =3;
FROM:
http://www.mysqlfaqs.net/mysql-faqs/Errors/1093-You-can-not-specify-target-table-comments-for-update-in-FROM-clause
Add your own comment.