如何在考虑以下场景的情况下在MySQL中选择多个范围:假设我想从我的产品表产品中取回
价格0 - 300和价格600-1000价格1200-1600
我正在尝试以下
SELECT * FROM products WHERE price > 0
AND price <= 300
AND price >= 600
AND price <= 1000
但不返回任何行
答案 0 :(得分:2)
如果某个数字介于0
和300
之间,则 不能介于600
和1000
之间。你在寻找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)