Last active
January 15, 2021 12:09
-
-
Save simonthompson99/69fad219bb78956e2aa030b2acc2eca4 to your computer and use it in GitHub Desktop.
[Update query with join] Update db table based on joins #sql #database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- update a table using a standard query | |
| with t as ( | |
| select <the primary key of the table> as rowid, <value to use to update> as update_value | |
| from <table> | |
| left join .... | |
| where .... | |
| ) | |
| update <table to update> | |
| set <field to update> = t.update_value | |
| from t | |
| where <pk in table> = t.rowid | |
| -- e.g: | |
| with t as ( | |
| select fc.id as rowid, false as uncheckable | |
| from consent_reading.field_check as fc | |
| left join consent_reading.spreadsheet ss | |
| on fc.spreadsheet_id = ss.id | |
| where fc.data_correct is null and ss.data_ingested = true | |
| ) | |
| update consent_reading.field_check | |
| set uncheckable = t.update_value | |
| from t | |
| where field_check.id = t.rowid | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment