I have an update query in postgres as such:
update table1 e
set (col1,col2) =
(select col1,col2 from table2 se
where e.id = se.id and se.col2 is not null);
However this updates all rows even the ones that are nulls for se.col2
. The rows are updated as nulls. I want to just update where se.col2 is not null
. Not all rows.
2
Answers
This is simple if you use the (non-standard)
FROM
clause ofUPDATE
:This assumes that
table2.id
is unique, or at least that no more than one row oftable2
matches any giventable1
row.DB Fiddle to play with.