Mysql查询从多个范围中选择

时间:2015-10-30 08:50:16

标签: mysql

我需要一些帮助。我试图在没有成功的情况下搜索答案/

我需要按价格字段从表中选择记录,该值必须在其中一个范围内。让我解释一下

select * from items 
    where color = 'black'
    AND shape = 'square'
    AND price between 333 and 444
    OR price between 777 and 888

但这会忽略颜色和形状条件。这个的正确语法是什么?这是一种正确的方法吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

and的运算符优先级高于or。您需要括号来管理

select * from items 
where color = 'black'
AND shape = 'square'
AND 
(
   price between 333 and 444
   OR price between 777 and 888
)

答案 1 :(得分:0)

我认为以下对您有用。请检查一次。

 select * from items 
        where color = 'black'
        AND shape = 'square'
        AND ((price between 333 and 444)
        OR (price between 777 and 888))