DBAtlas · Database operations atlas

1093: You can't specify target table 'x' for update in FROM clause

MySQL

What does it mean?

In an UPDATE or DELETE statement, the table being updated/deleted cannot itself be used directly as a source in a subquery (in FROM). MySQL does not allow the same table to be both target and source this way.

Why does it happen?

A subquery reading the same target table directly, as in UPDATE my_table ... WHERE id IN (SELECT ... FROM my_table ...); a subquery referencing the same table in a DELETE.

How to fix it

Wrap the subquery in a derived table and give it a new alias so MySQL treats it as a separate copy: UPDATE my_table SET c = 1 WHERE id IN (SELECT id FROM (SELECT id FROM my_table WHERE condition) AS t); Alternatively, write it as a self-join: UPDATE my_table a JOIN my_table b ON ... SET a.c = ...;

Open in DBAtlas →

More from this engine