Skip to content

Instantly share code, notes, and snippets.

@RodolfoSilva
Forked from aknosis/gist:1323675
Last active August 29, 2015 14:06
Show Gist options
  • Save RodolfoSilva/898c538fbff32ee41367 to your computer and use it in GitHub Desktop.
Save RodolfoSilva/898c538fbff32ee41367 to your computer and use it in GitHub Desktop.
-- Example of how to include an if conditional in a where clause
-- We only want rows where a = 1 or 2 but only a = 2 if b = 4
-- table structure:
-- [ a | b ]
-- 1 4
-- 2 4
-- 1 5
-- 3 9
-- 2 7
-- 2 33
SELECT * FROM table
WHERE a IN (1,2) -- a is 1 or 2
AND (a <> 2 OR a = 2 AND b = 4) -- a isn't 2 OR it is 2 and b is 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment