Skip to content
Advertisement

SQL – Update multiple tables, the same column across one query

I have a postgres database

There are 3 tables, I’m trying to make a sql UPDATE statement that updates an active column in each table.

Table1 has a field record that looks like this:

Table2

Table3 is the same as table 2

I need a query that looks for the name “Digital Only” and it’s ID on table1. Update it’s active column. Update corresponding active columns in the 2 other tables matching the original ID in table1

The tables have pretty long names so ideally I want to alias them:

So far I have something along the lines of this in pseudocode

Not sure how to do this. The table relationships look like this: enter image description here

Advertisement

Answer

I think this does what you want. The idea is first update table1, and to use the returning clause to return the table1_id, that can be used to update the two other tables:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement