mysql价格区间在或在范围内

时间:2014-01-10 11:15:20

标签: php mysql

我有一张表products,其中包含价格和销售列(浮动8,2)。 没有销售价格的产品将具有如下所示的值。 price 13.20 sale 0.00

产品的销售价格如下: price 10.00 sale 08.00

我正在尝试将price介于$ min和$ max之间或sale介于$ min和$ max之间的所有产品。

 select * from `products`  where       

((price BETWEEN 0.00和22.00)或(sale介于0.00和22.00之间))
  status = 1和sold = 0和deleted = 0订单id desc limit 12

但这不起作用?任何人都可以帮我解决这个问题吗?提前谢谢。

好的我测试了这个 -

 select * from `products`  where       

((price BETWEEN 0.00和22.00)或(sale介于0.00和22.00之间))
  status = 1和sold = 0和deleted = 0订单id desc limit 12

问题我发现如果最低价格是例如0.01,但是如果最低价格是0.00,我会得到没有价格限制的产品。为什么查询讨厌0.00分钟的价格? OO

3 个答案:

答案 0 :(得分:0)

我想,你忘了OR子语句之间的括号:

select * from `products`
where (
    (`price` >= $min AND `price` <= $max) OR (`sale` >= $min AND `sale` <= $max)
) and `status` = 1 and `sold` = 0 and `deleted` = 0 order by `id` desc limit 12

答案 1 :(得分:0)

你能试试吗,添加了BETWEEN min AND max

  select * from `products`  where       
  ( (`price` BETWEEN $min AND $max) OR (`sale` BETWEEN $min AND $max) )       
  and `status` = 1 and `sold` = 0 and `deleted` = 0 order by `id` desc limit 12

答案 2 :(得分:0)

您可以使用BETWEEN

select * from `products` where (price BETWEEN $min AND $max) OR `sale` >= $min AND `sale` <= $max) and `status` = 1 and `sold` = 0 and `deleted` = 0 order by `id` desc limit 12