Update multiple rows to different values - Gandalf's Blog

โ† All posts

โ€‹ - โ€‹

Update multiple rows to different values

#postgresql

Think it's impossible to apply different values to different rows with just one PostgreSQL query? Think again.

Check this out:
sql
UPDATE my_table
    SET foo = my_table.foo + new_table.foo
    FROM (VALUES (1, 5), (2,6), (3,7)) AS new_table(id, foo)
    WHERE my_table.id = new_table.id;