MySQL查询过滤正确,有些条件被忽略

时间:2016-05-30 18:50:21

标签: mysql

我得到了以下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

我的结果是:它符合city = 3 or city=1 or city=2

的条件

但它忽略了其余部分:and rent = 1 and bedrooms = 1 and type = 1

我希望结果包含多个城市,但需要满足要求:and rent = 1 and bedrooms = 1 and type = 1,现在它只输出多个城市的结果,而不再按租借进行过滤,卧室类型

请帮助,谢谢。

3 个答案:

答案 0 :(得分:1)

由于您使用多个OR条件,因此最好使用IN子句。

SELECT * FROM LISTING 
WHERE paused = 0
AND city IN (1, 2, 3)
AND rent = 1 
AND bedrooms = 1 
AND type = 1

答案 1 :(得分:0)

您需要划分OR部分

SELECT * FROM LISTING WHERE 

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

更好的方法是:

SELECT * FROM LISTING WHERE 

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

答案 2 :(得分:0)

将其更改为:

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;