UPDATE SQL statement with a join statement
Steal from Stackoverflow, to update multiple rows of first table by using value from second table, where the relationship between two tables are defined at JOIN statement.
UPDATE
Table SET
Table.col1 = other_table.col1,
Table.col2 = other_table.col2 FROM
Table INNER JOIN
other_table ON
Table.id = other_table.id WHERE
Table.col1 != other_table.col1 or
Table.col2 != other_table.col2 or
(other_table.col1 is not null and table.col1 is null) or
(other_table.col2 is not null and table.col2 is null)
UPDATE
Table SET
Table.col1 = other_table.col1,
Table.col2 = other_table.col2 FROM
Table INNER JOIN
other_table ON
Table.id = other_table.id WHERE
WHERE EXISTS(SELECT Table.Col1, Table.Col2 EXCEPT SELECT other_table.Col1, other_table.Col2)
Comments