HellOfACode HellOfACode - 2 months ago 14
SQL Question

Postgres Update multiple rows

I need to update multiple rows in a table.

i have table

customer
and
contact


I need to update
customer
where the linked contact in the
contact
table has the column
city
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

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"
)
Comments