postgresql select 해서 update 하기 (join 활용) :: 개발/일상_Mr.lee

postgresql select 해서 update 하기 (join 활용)

Posted by Mr.mandu.
2017. 12. 15. 15:41 개발/DB

안녕하세요.

예전에 select 해서 insert 하기라는 포스팅을 한 기억이 나네요


[개발/database] - [oracle]오라클 select해서 insert 하기


이번에는 select 해서 update하기 입니다.

사실 말 그대로 select로 update를 하고자 하면

update tableA set


addr = (select addr from tableB where usr_nm='홍길동')


이런식으로 할수도 있고


update tabeA a set


addr = (select addr from tableB where usr_nm = a.usr_nm)


where 


usr_nm in 


(

  select usr_no from tableB 

)



이런식으로도 할 수 있습니다.

하지만 데이터 양이 많아지면 많아 질수록 속도는 엄청나게 느려집니다.

select를 직접 해오기 때문이죠.

그래서 join을 통해 update 하시는걸 추천 드립니다.


join 해서 update하기

바로 예시쿼리 적겠습니다.

update tableA AS a set
addr = b.addr
from tableB AS b
where
a.usr_no = b.usr_no

select 해서 update문을 돌려야하는데
어떻게 할지 한참을 고민했네요...
다들 삽질하지 맙시다!

현재 저는 postgresql에서 테스트를 하였지만
오라클, mysql에서도 무난하게 쓸 수 있을거라 생각합니다.
감사합니다.