Skip to content

Instantly share code, notes, and snippets.

@FernandoBasso
Created September 17, 2016 14:42
Show Gist options
  • Save FernandoBasso/f945df3a3545b45e884aef849d37ada8 to your computer and use it in GitHub Desktop.
Save FernandoBasso/f945df3a3545b45e884aef849d37ada8 to your computer and use it in GitHub Desktop.
Use of INSERT and RETURNING in pgsql
INSERT INTO images (
post_id
, extension
, position
, created_at)
(SELECT
'13'
, 'jpg'
, COALESCE(MAX(position), 0) + 1
, CURRENT_TIMESTAMP
FROM images
WHERE post_id = '13')
RETURNING id, extension, position;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment