Postgres query to update a column with value of another
I have a query that is working: But, I'm not sure if it is the best.
update table1 set column1 = column2, column2 = 0 where id = 'X';
As you can see, I set the value of column2 to column1, and in the same query, I set column2 to zero.
Is that the best option to do this?
Bogopo last edited by
There is nothing wrong with this update, and it’s actually the preferred way to accomplish this type of update. With this method, you touch the table once instead of twice.
When setting a column to the value of another column, the value of column2 at the beginning of the update is the value applied to column1, even if you’re also updating column2 during the same update.