HellOfACode HellOfACode - 1 year ago 100
SQL Question

Postgres Update multiple rows

I need to update multiple rows in a table.

i have table


I need to update
where the linked contact in the
table has the column
at a certain value.
I can get the rows i need with this query

Select cus.id, con.city
from customer cus, contact con
where cus.contacts_id=con.id
and con.city="MyValue"

I know how to update one table but do not understand how to update the table when the rows are looked up from a different table.

Answer Source

Firstly, please do not use the old JOINs (FROM comma separated tables).

Secondly, here you go:

UPDATE customer SET whatever = 'whatever value'
WHERE contacts_id IN (
    SELECT id FROM contact WHERE city="MyValue"