MySQL & MariaDB error 1093: You can't specify target table for update in FROM clause
You can't specify target table 'carts' for update in FROM clause (5.6.51) MySQL won't let an UPDATE or DELETE read the same table in a subquery that it is modifying. MariaDB (10.3.2+) allows it; on MySQL you materialise the subquery first or use a multi-table statement.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | ER_UPDATE_TABLE_USED | ER_UPDATE_TABLE_USED |
| SQLSTATE | HY000 | HY000 |
| Type | Server error | Server error |
| Versions | every release since 5.0 active | every release since 5.5 active |
What it means
You can't specify target table 'emp' for update in FROM clause. A statement like DELETE FROM emp WHERE salary < (SELECT AVG(salary) FROM emp) reads and changes emp at the same time; MySQL refuses rather than risk the subquery seeing half-changed data.
Why it happens
UPDATEorDELETEwith aWHERE … IN (SELECT … FROM same_table)or a scalar subquery on the same table: removing duplicates, deleting below-average rows, “keep the latest row per group”.- It is MySQL-only: our tests show 1093 on every MySQL version from 5.6 to 9.x, while MariaDB 10.6–11.8 runs the same statement correctly (supported since MariaDB 10.3.2).
How to fix it
- Wrap the subquery in a derived table so it is computed before the change starts:
DELETE FROM emp WHERE salary < (SELECT a FROM (SELECT AVG(salary) AS a FROM emp) x); - Or use a multi-table statement:
DELETE e FROM emp e JOIN (SELECT AVG(salary) AS a FROM emp) x ON e.salary < x.a; - On MySQL 8.0 the optimizer may merge a simple derived table back into the outer query; if the error returns, add
/*+ NO_MERGE(x) */or use a temporary table.
Reproduce it yourself
These steps are the exact test our harness runs. Use a scratch server, never production.
Connect with
mysql -u root -pand create a scratch database and the test data:CREATE DATABASE demo; USE demo; CREATE TABLE emp (id INT PRIMARY KEY, salary INT); INSERT INTO emp VALUES (1, 100), (2, 300), (3, 200);Run:
DELETE FROM emp WHERE salary < (SELECT AVG(salary) FROM emp);
You get: You can't specify target table 'emp' for update in FROM clause(9.7.4)
Verified fixes
Wrap the subquery in a derived table so it is materialised first
DELETE FROM emp WHERE salary < (SELECT a FROM (SELECT AVG(salary) AS a FROM emp) x); SELECT id FROM emp ORDER BY id;Or rewrite as a multi-table DELETE ... JOIN
DELETE e FROM emp e JOIN (SELECT AVG(salary) AS a FROM emp) x ON e.salary < x.a; SELECT id FROM emp ORDER BY id;
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| UPDATE/DELETE with a subquery that reads the same table repro | ✓ 1093 | ✓ 1093 | ✓ 1093 | ✓ 1093 | ✓ 1093 | ≠ | ≠ | ≠ | ≠ |
| Wrap the subquery in a derived table so it is materialised first fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| Or rewrite as a multi-table DELETE ... JOIN fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
What the warnings and failures say (1)
- UPDATE/DELETE with a subquery that reads the same table (MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): expected error 1093 did not occur
Related errors
Where this error shows up in our guides
- UPDATE with a join · UPDATE ... WHERE col IN (SELECT ... FROM same table): 1093 on MySQL, allowed on MariaDB MySQL 5.6 5.7 8.0 8.4 9
How the message changed between versions
| MariaDB from | Symbol | Message |
|---|---|---|
| 5.5.68 | ER_UPDATE_TABLE_USED | You can't specify target table '%-.192s' for update in FROM clause |
| 10.0.38 | ER_UPDATE_TABLE_USED | Table '%-.192s' is specified twice, both as a target for '%s' and as a separate source for data |