검색결과 리스트
MySQL에 해당되는 글 1건
- 2010.02.19 조건에 맞게 select 한 후 update 시키기
글
UPDATE
table1 a,(
select col1, col2
from table 2
where col1 = 1
) b
SET
a.col1 = b.col1
a.col2 = b.col2
WHERE
a.col1 = 111
이런식으로 써주면 됩니다.
그런데 한가지 문제점.
b에서 select된 결과가 null일 경우엔 update가 되지 않는데, 이러한 경우엔 left join을 걸어주면 됩니다.
UPDATE
table1 a
left join(
left join(
select col1, col2, col3
from table 2
where col1 = 1
from table 2
where col1 = 1
) b on a.col1 = b.col3
SET
a.col1 = b.col1
a.col2 = b.col2
WHERE
a.col1 = 111
이런식으로...