Friday, December 01, 2006

Survival: sql, update a table with values from another table

Often I have to update a table column with values taken from another table.

Since everytime I write this from scratch, and I'm lazy, I write it down here!

UPDATE table_one
SET column_one = ( SELECT table_two.column_two
 FROM table_two
 WHERE table_one.id = table_two.id)
WHERE EXISTS
  ( SELECT table_two.id
    FROM table_two
    WHERE table_two.id = table_one.id);

No comments: