2014-01-10 99 views
0

我有一個表products與價格和銷售列(浮動8,2)。 沒有銷售價格的產品將具有如下所示的值。 price 13.20 sale 0.00mysql價格範圍有或在範圍內

而作爲產品的銷售價格: price 10.00 sale 08.00

我試圖讓所有的產品,其中price爲$ min和$最大或sale之間爲$分鐘之間和$最大。

select * from `products` where  

((price BETWEEN 0.00 AND 22.00)OR(sale BETWEEN 0.00與22.00))
status = 1和sold = 0和deleted = 0 ORDER BY id DESC LIMIT 12

但這不管用?任何人都可以幫我解決這個問題嗎?提前致謝。

確定我測試這一個 -

select * from `products` where  

((price BETWEEN 0.00 AND 22.00)OR(sale BETWEEN 0.00與22.00))
status = 1和sold = 0和deleted = 0爲了通過id降序極限12

問題我發現它的工作原理如果最低價格是例如0.01,但如果最低價格是0.00我得到沒有價格限制的產品。爲什麼查詢討厭0.00分鐘的價格? OO

回答

0

我認爲,你忘了substatements之間括號或:

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