Kzone
Kzone

Reputation: 67

Postgresql : Insert values from another table while matching id's

I'm trying to build following query :

INSERT INTO table1 (field1, field2)
VALUES (field1, field2) FROM table2
WHERE table2.id = table1.id

Logically I get a "more than one row returned by a subquery used as an expression" error because of the WHERE condition, but can't get the clue how to solve it, nor find matching case in existing posts as for now. I've tested this approach but it fails ...

Thanks for any help

Upvotes: 4

Views: 1909

Answers (1)

forpas
forpas

Reputation: 164099

I suspect that what you actually want is update the rows of table1 with the matching rows of table2:

UPDATE table1 AS t1
SET (field1, field2) = (t2.field1, t2.field2)
FROM table2 AS t2
WHERE t2.id = t1.id

Upvotes: 7

Related Questions