在mysql中选择更多范围

时间:2013-12-05 09:01:56

标签: mysql sql range

如何在考虑以下场景的情况下在MySQL中选择多个范围:假设我想从我的产品表产品中取回

价格0 - 300和价格600-1000价格1200-1600

我正在尝试以下

SELECT * FROM products WHERE price > 0 
  AND price <= 300 
  AND price >= 600 
  AND price <= 1000 

但不返回任何行

4 个答案:

答案 0 :(得分:2)

如果某个数字介于0300之间,则 不能介于6001000之间。你在寻找OR吗?或许BETWEEN只是为了让事情变得更容易?

SELECT * FROM products WHERE
    price BETWEEN 0 AND 300
    OR
    price BETWEEN 600 AND 1000
    OR
    price BETWEEN 1200 AND 1600

答案 1 :(得分:1)

SELECT * FROM products WHERE 
(price > 0 AND price <= 300 )
OR
(price > 600 AND price <= 1000)
OR
(price > 1200 AND price <= 1600 )

你也可以在

之间使用
price BETWEEN 0 AND 300
or
price between ...

考虑price BETWEEN 0 AND 300之间将包含0 它将被解析为price >= 0 AND price <= 300

答案 2 :(得分:0)

使用'或'条件而不是'和'

答案 3 :(得分:0)

你当然需要这样的东西:

SELECT *
FROM products
WHERE
    (price > 0 AND price <= 300) OR
    (price > 600 AND price <= 1000) OR
    (price > 1200 AND price <= 1600)