Skip to content

Instantly share code, notes, and snippets.

@peacefixation
Created March 27, 2019 04:31
Show Gist options
  • Save peacefixation/1da867686ff4c1ede930c51752e757be to your computer and use it in GitHub Desktop.
Save peacefixation/1da867686ff4c1ede930c51752e757be to your computer and use it in GitHub Desktop.
Postgres update join
-- given two tables with a foreign key
-- and the requirement to set a field in tableA to the value of the corresponding field in tableB
-- in this case setting tableA.someField to tableB.someOtherField where tableA.fk_id = tableB.id
-- tableA (
-- id
-- someField
-- fk_id
-- )
-- tableB (
-- id
-- someOtherField
-- )
UPDATE tableA
SET someField = tableB.someOtherField
FROM tableB
WHERE tableA.fk_id = tableB.id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment