Skip to content

Instantly share code, notes, and snippets.

@rikonor
Created August 18, 2016 21:12
Show Gist options
  • Save rikonor/834f05943834935a3339415447fcd24d to your computer and use it in GitHub Desktop.
Save rikonor/834f05943834935a3339415447fcd24d to your computer and use it in GitHub Desktop.
WITH
OldEntry AS (
SELECT domain_id FROM domains WHERE domain = 'google.com'
),
NewEntry AS (
INSERT INTO domains (domain)
SELECT 'google.com'
WHERE NOT EXISTS (
SELECT domain_id FROM OldEntry
)
RETURNING domain_id
)
SELECT domain_id FROM NewEntry
UNION ALL
SELECT domain_id FROM OldEntry;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment