Created
October 28, 2011 21:52
-
-
Save aknosis/1323675 to your computer and use it in GitHub Desktop.
Conditional inside WHERE clause
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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