Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save berkayakcay/c1ef0848ce66be314060fb465b2cc23f to your computer and use it in GitHub Desktop.
Save berkayakcay/c1ef0848ce66be314060fb465b2cc23f to your computer and use it in GitHub Desktop.
PostgreSQL UPDATE table and get ids from UPDATED rows then INSERT INTO another table
/*
PostgreSQL
If you want to insert any column retaled with update query you can use this.
It gets ids from updated tables and then inter them into another table.
*/
with rows AS (
UPDATE product SET price = price * 1.10
RETURNING id
)
INSERT INTO updated_product
SELECT * FROM rows;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment