Mikail Mikail - 6 months ago 9
SQL Question

MySQL query filter properly , some where conditions are ignored

I got the following sql :

SELECT * FROM LISTING WHERE

paused = 0 and city = 3 or city = 1 or city = 2
and rent = 1 and bedrooms = 1 and type = 1


my result is that: it meets the condition that
city = 3 or city=1 or city=2


but it ignores the rest :
and rent = 1 and bedrooms = 1 and type = 1


I'd like the result to have multiple cities but the requirements:
and rent = 1 and bedrooms = 1 and type = 1
to be met, right now it just outputs results for multiple cities with no further filter by rent, bedrooms, type .

please help, Thank You.

Answer

Change it to:

SELECT * FROM LISTING WHERE 

paused = 0   and ( city = 3 or city = 1 or city = 2 )
and rent = 1 and bedrooms = 1 and type = 1;

OR

SELECT * FROM LISTING WHERE 

paused = 0   and city IN (1,2,3)
and rent = 1 and bedrooms = 1 and type = 1;
Comments