Skip to content

Instantly share code, notes, and snippets.

@simonthompson99
Last active January 15, 2021 12:09
Show Gist options
  • Select an option

  • Save simonthompson99/69fad219bb78956e2aa030b2acc2eca4 to your computer and use it in GitHub Desktop.

Select an option

Save simonthompson99/69fad219bb78956e2aa030b2acc2eca4 to your computer and use it in GitHub Desktop.
[Update query with join] Update db table based on joins #sql #database
-- 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