HellOfACode
HellOfACode

Reputation: 1743

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.

Upvotes: 0

Views: 4369

Answers (1)

Boris Schegolev
Boris Schegolev

Reputation: 3701

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

Upvotes: 2

Related Questions