Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

insert

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

like image 509
Kzone Avatar asked Oct 16 '25 03:10

Kzone


1 Answers

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
like image 83
forpas Avatar answered Oct 18 '25 17:10

forpas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!