Md Abdullahil Shafi
← All posts

Postgres update multiple rows based on multiple conditions in single query

PostgresSQLTipsTricks

When you need to update multiple rows based on multiple conditions, ever wonder if that can be done in a single query?

Here is a solution:

update account 
set currency = nv.currency
from
    ( values
        (12, 'EURO'),
        (18, 'DOLLAR'),
        (13, 'Pound')
    ) as nv (id, currency)
where account.id = nv.id ;

Credit goes to this dba.stackexchange.com answer.