【问题标题】:mysql price range with or in rangemysql价格范围与或在范围内
【发布时间】:2014-01-10 11:15:19
【问题描述】:

我有一张表 products 带有价格和销售列(浮动 8,2)。 没有销售价格的产品将具有如下所示的值。 price 13.20 sale0.00

销售价格为: price 10.00 sale08.00

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

 select * from `products`  where       

(((price 0.00 和 22.00 之间)或(sale 0.00 和 22.00 之间))
status = 1 和sold = 0 和deleted = 0 按id desc 限制12 排序

但这不起作用?谁能帮我解决这个问题?提前致谢。

好的,我测试了这个 -

 select * from `products`  where       

(((price 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

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    我认为,您忘记了 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
    

    【讨论】:

      【解决方案2】:

      你可以试试这个吗,已添加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
      

      【讨论】:

        【解决方案3】:

        您可以使用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
        

        【讨论】:

          猜你喜欢
          • 2016-06-10
          • 1970-01-01
          • 1970-01-01
          • 2011-12-04
          • 2011-01-01
          • 2011-11-06
          • 1970-01-01
          • 2015-12-25
          • 1970-01-01
          相关资源
          最近更新 更多