Programming/SQL

[MySQL]You can't specify target table "table name" for update in FROM clause

옥수수빵 2026. 9. 11. 11:10
728x90

Suppose you have a query like this:

INSERT INTO member (mno, mname, ...) VALUES((SELECT MAX(mno) + 1 FROM member), 'John Smith', ...);

Looks harmless enough. But when you actually run it, MySQL throws an error along the lines of the one in the title.
MySQL restricts subqueries that reference the same table you're inserting into (or updating). The fix is simple — just give the subquery's table an alias:

INSERT INTO member (mno, mname, ...) VALUES((SELECT MAX(m.mno) + 1 FROM member AS m), 'John Smith', ...);
반응형