Skip to content

Instantly share code, notes, and snippets.

@jkatz
Created June 5, 2012 03:08
Show Gist options
  • Save jkatz/2872338 to your computer and use it in GitHub Desktop.
Save jkatz/2872338 to your computer and use it in GitHub Desktop.
CREATE TABLE a (id serial, x int);
INSERT INTO a (x) VALUES (5);
INSERT INTO a (x) VALUES (7);
INSERT INTO a (x) VALUES (11);
INSERT INTO a (x) VALUES (13);
--- first data
SELECT * FROM a;
--- return the old data rows from the result
WITH old_data AS (
SELECT id, x
FROM a
)
UPDATE a
SET x = 1
FROM old_data
WHERE old_data.id = a.id
RETURNING old_data.x;
-- show that data has been updated
SELECT * FROM a;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment