Error Code: 1093. You can't specify target table - mysql 단일 동일 테이블 실행 시 오류 해결 법

728x90

mySQL은 update나 delete 시 동일 테이블을 사용하는 것이 불가능 하다.

 

따라서 tmp라는 일회성 테이블을 생성하여 서브쿼리로 집어넣으면 실행이 가능함

 

동일 table sum값을 다시 동일테이블 column에 update해야할 일이 있어서 아래와 같이 작성하였더니 위와 같은 오류가 발생함.

update mentiboard set boardlike = 
    (select sum(boardLike)+1 as boardLikes from mentiboard where num = 23);

 

따라서 위와 같이 설명한대로 서브쿼리를 하나 더 집어넣어서 tmp라는 테이블로 임시 저장한 후에 동일하게 실행하였더니 문제없이 되는 중

update mentiboard set boardlike = 
	(select sum(boardLike)+1 from
    	(select sum(boardLike)+1 as boardLikes from mentiboard where num = 23)tmp);
728x90

'국비과정 > DB - mySQL' 카테고리의 다른 글

다른 DB테이블 복사하기  (0) 2022.04.20
DB에서 중복행 제거하는 쿼리  (0) 2022.04.09
FOREIGN KEY의 사용법, 예제  (0) 2022.03.31
DB의 정규화  (0) 2022.03.21
DB - mySQL을 JAVA와 연동하기  (0) 2022.03.16