skcrpk
skcrpk

Reputation: 556

how do i filter a column with multiple values

how do i filter a column col1 with multiple values

select * from table where col1=2 and col1=4 and  userID='740b9738-63d2-67ff-ba21-801b65dd0ae1' 

i tired

select * from table where col1=2 or col1=4 and  userID='740b9738-63d2-67ff-ba21-801b65dd0ae1'

the result of both queries is incorrect

the first one gives zero results second one gives 3 results

the correct is 2 results

the sql will run against the sqlite db.

Upvotes: 0

Views: 59

Answers (1)

D Stanley
D Stanley

Reputation: 152511

AND is evaluated before OR, so your query is equivalent to:

select * 
from table 
where col1=2 or (col1=4 and  userID='740b9738-63d2-67ff-ba21-801b65dd0ae1')

You need to explicitly group the conditions when mixing AND and OR:

select * 
from table 
where (col1=2 or col1=4) and  userID='740b9738-63d2-67ff-ba21-801b65dd0ae1'

Upvotes: 2

Related Questions