알아두기/SQL

mysql) 조인테이블 업데이트 관련 에러

값을변경 2023. 1. 19. 13:35

Every derived table must have its own alias
: MYSQL은 서브쿼리에 별칭이 있어야 함.



Error Code: 1248. Every derived table must have its own alias
Error Code: 1288. The target table A of the UPDATE is not updatable
Error Code: 1054 Unknown column '~~' in 'where clause'


두 개의 테이블을 한꺼번에 update 하고 싶어서 찾아봤음.

update (
	select * 
    	from 
	      table1 as a, table2 as b 
    	where 
	      a.idx = b.idx ) 
set 
	column1 = '티스토리' ;

이런 구조 였음.

mysql은 서브쿼리에 '별칭'을 줘야 함. 쓰던 안 쓰던
안 그러면 에러남. Error Code: 1248. Every derived table must have its own alias

update (
	select * 
    	from 
		table1 as a, table2 as b 
    	where 
		a.idx = b.idx 
        ) as T
set 
	column1 = '티스토리' ;

별칭을 주었더니 나중에 set 할 때 column은 T테이블의 것인가?라고 생각해서
set을 t.column으로 함. 그래서 에러남. Error Code: 1288. The target table A of the UPDATE is not updatable

그 뒤로 시도했지만 Error Code: 1054 Unknown column '~~' in 'where clause' 이런 에러들이 주르륵 나옴.

힌트를 얻은 문구.
두 개의 테이블을 합쳐서 하나의 가상 테이블을 만들고, 거기서 update.

UPDATE table1 as a, table2 as b 
SET 
	column1 = '1',
	column2 = '2'
WHERE 
	a.idx = b.idx
  AND
    ...#추가조건 ;

간단하게 생각해서 이런 식의 문구를 해봤는데 됨. ㅋㅋㅋㅋㅋ