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', ...);
반응형
'Programming > SQL' 카테고리의 다른 글
| [MySQL]You can't specify target table "테이블명" for update in FROM clause (1) | 2024.04.03 |
|---|---|
| [MySQL]TIMESTAMPDIFF() 시간, 날짜 차이 (0) | 2022.12.01 |
| [MySQL]특정 조건에 포함되지 않는 모든 데이터 삭제하기 (0) | 2022.06.16 |
| 테이블 구조(MySQL) 쿼리로 갖고 오기 (0) | 2022.02.04 |
| [MySQL]정렬 순서가 있는 게 먼저 나오고 없는 게 나중에... (0) | 2021.12.09 |

