Question
Is there a way to select data where any one of multiple conditions occur on the same field?
Example: I would typically write a statement such as:
select * from TABLE where field = 1 or field = 2 or field = 3
Is there a way to instead say something like:
select * from TABLE where field = 1 || 2 || 3
Any help is appreciated.
Solution
Sure thing, the simplest way is this:
select foo from bar where baz in (1,2,3)
OTHER TIPS
select * from TABLE where field IN (1,2,3)
You can also conveniently combine this with a subquery that only returns one field:
select * from TABLE where field IN (SELECT boom FROM anotherTable)
OR:
SELECT foo FROM bar WHERE baz BETWEEN 1 AND 3
select * from TABLE where field in (1, 2, 3)
WHERE field IN (1, 2, 3)
You can still use in for
select *
from table
where field = '1' or field = '2' or field = '3'
its just
select * from table where field in ('1','2','3')
while in
is a shortcut for or
and I wasn't sure how I could combine in
with and
, I did it this way
SELECT * FROM table
WHERE column1='x' AND (column2='y' OR column2='z');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow