Search code examples
sqlpostgresqlsql-update

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!


Solution

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

    update table1
        set col1 = . . .
        from table2
        where table1.id = table2.table1_id