Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update postgresql table with values from another table

Let's say I have table table1 with columns id, value and table2 with columns table1_id, value.

How would I write Postgresql query to update table1.value (whole table, not just one row) with table2.value if they are matching on table1.id = table2.table1_id?

Thank you for answers in advance!

like image 365
user2740217 Avatar asked Aug 03 '17 19:08

user2740217


1 Answers

You use a from clause. In Postgres, this looks like:

update table1
    set col1 = . . .
    from table2
    where table1.id = table2.table1_id
like image 64
Gordon Linoff Avatar answered Oct 14 '22 19:10

Gordon Linoff