ERROR 1093 (HY000): You can't specify target table '%s' for update in FROM clause
ERROR 1093 (HY000): You can't specify target table '%s' for update in FROM clause
Explanation
The MySQL UPDATE statement doesn't allow subqueries against the same table that is being updated. IE: The following will fail with the 1093 error message:
UPDATE TABLE_A
WHERE col IN (SELECT col2
FROM TABLE_A)
...or:
UPDATE TABLE_A
SET ...
WHERE col = (SELECT col2
FROM TABLE_A)
Solutions
There are two means of solving this issue:
MySQL specific
- Use the ANSI-92 JOIN syntax:UPDATE TABLE_A a JOIN TABLE_A b ON b.col2 = a.col SET ...
Bury the subquery one level deeper
UPDATE TABLE_A SET ... WHERE col = (SELECT x.col FROM (SELECT col2 AS col FROM TABLE_A) AS x)