When encountering the error 1093 in MySQL, indicating "Can't specify target table for update in FROM clause," the immediate action to take is to modify the query to avoid directly referencing the target table in a subquery within the FROM clause. Use a workaround by employing an intermediate subquery or a temporary table. Here are two approaches:
If your original query looks something like this:DELETE FROM my
table WHERE id IN (SELECT id FROM my
table WHERE condition);
Modify it to use an intermediate subquery like so:DELETE FROM my
table WHERE id IN (SELECT id FROM (SELECT id FROM my
table WHERE condition) AS subquery);
Create a temporary table to store the intermediate result:CREATE TEMPORARY TABLE temp
ids AS SELECT id FROM my
table WHERE condition;
Then perform the operation using this temporary table:DELETE FROM my
table WHERE id IN (SELECT id FROM temp
ids);
After the operation, drop the temporary table if necessary:DROP TEMPORARY TABLE IF EXISTS temp_ids;
These actions circumvent the limitation by not directly referencing the target table in the FROM clause of the subquery.
Let Dr. Droid create custom investigation plans for your infrastructure.
Book Demo