Skip to content

Instantly share code, notes, and snippets.

@jueyang
Last active August 29, 2015 14:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jueyang/1c00f0767e0ab7fa5746 to your computer and use it in GitHub Desktop.
Save jueyang/1c00f0767e0ab7fa5746 to your computer and use it in GitHub Desktop.
$insert = "INSERT INTO spider_count (spider, tally) SELECT 'Googlebot', 1";
$upsert = "UPDATE spider_count SET tally=tally+1 WHERE date='today' AND spider='Googlebot'";
WITH upsert AS ($upsert RETURNING *) $insert WHERE NOT EXISTS (SELECT * FROM upsert);
--- expanded
WITH upsert AS (
UPDATE spider_counts
SET tally=tally+1
WHERE date='today' AND spider='Googlebot'
RETURNING *
)
INSERT INTO spider_counts (spider, tally)
SELECT 'Googlebot', 1
WHERE NOT EXISTS (
SELECT * FROM upsert
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment