2015-10-30 63 views
0

我需要一些幫助。我試圖搜索沒有成功的答案/從多個範圍中進行選擇的Mysql查詢

我需要從價格字段中選擇記錄,該值必須位於其中一個範圍內。讓我解釋一下

select * from items 
    where color = 'black' 
    AND shape = 'square' 
    AND price between 333 and 444 
    OR price between 777 and 888 

但是這會忽略顏色和形狀的條件。什麼是正確的語法?這是一個正確的方法嗎?

謝謝!

回答

1

and的運算符優先級高於or。你需要括號來管理,

select * from items 
where color = 'black' 
AND shape = 'square' 
AND 
(
    price between 333 and 444 
    OR price between 777 and 888 
) 
+0

謝謝。這是一個問題。 – stakantin

0

我覺得下面對你有用。請檢查一次。

select * from items 
     where color = 'black' 
     AND shape = 'square' 
     AND ((price between 333 and 444) 
     OR (price between 777 and 888))