DB : MySQL | Oracle | MyBatis

MySQL Error Code: 1093. You can't specify target table 'table_name' for update in FROM clause 에러 해결

기매_ 2023. 1. 9. 17:03
update comment set
	good=((select good from comment where id=2)+1)
where id=2;

MySQL에서 이걸 실행했더니

 

Error Code: 1093. You can't specify target table 'comment' for update in FROM clause

이 에러가 떴다...


원인 및 해결방법

원인 : MySQL 은 Oracle 과는 달리 UPDATE 나 DELETE 시 자기(본인) 테이블의 데이터를 바로 사용하지 못하므로 이러한 SQL 을 실행시 1093 에러가 발생함.
해결방법 : Sub Query 를 하나 더 넣고 sub query 결과를 임시 테이블로 만든후에 실행하면 됨.


 

update comment set
	good=((select tmp_table.nowgood from (select good nowgood from comment where id=2) tmp_table)+1)
where id=2;

따라서 이렇게 바꾼 후 실행하였더니 정상적으로 작동되었다 !

 

 


참고

https://www.lesstif.com/dbms/mysql-error-1093-you-can-t-specify-target-table-tablename-for-update-in-from-clause-18220088.html

 

MySQL Error 1093 : You can't specify target table 'tablename' for update in FROM clause

 

www.lesstif.com