Wednesday, November 20, 2013

Update matching rows in one table with data from another table

Update matching rows in t1 with data from t2

update t1
set (c1, c2, c3) =
(select c1, c2, c3 from t2
 where t2.user_id = t1.user_id)
where exists
(select * from t2
 where t2.user_id = t1.user_id)

The "where exists" part it to prevent updating the t1 columns to null where no match exists.

No comments:

Post a Comment