2013-07-02 104 views
0

我運行下面的查詢的MySQL不返回預期

SELECT * FROM (`default_schemes`) 
WHERE `region` = 'Location1' 
OR `region` = 'Location2' 
OR `region` = 'Location3' 
OR `region` = 'Location4' 
OR `region` = 'Location5' 
OR `region` = 'Location6' 
AND `finance` LIKE '%Equity%' 
OR `guidance` LIKE '%Business Mentoring %' 

將匹配的金融和指導,但在什麼設置的所有位置regarless拉,我缺少什麼?

+0

某些示例數據和預期結果可能會有所幫助 – chetan

+0

缺少一些括號可能... – John3136

+0

AND OR子句的操作順序不明確。請使用括號。 :) – sav

回答

2
SELECT * FROM `default_schemes` 
WHERE `region` in ('Location1' ,'Location2' ,'Location3' , 'Location4' , 'Location5' ,'Location6') 
AND (`finance` LIKE '%Equity%' 
OR `guidance` LIKE '%Business Mentoring%'); 
+0

我是如此盲目,只是工作了,因爲你張貼,謝謝 – Brob

0

也許你想這個...

SELECT * 
    FROM default_schemes 
WHERE region IN('Location1','Location2','Location3','Location4','Location5','Location6') 
    AND (finance LIKE '%Equity%' OR guidance LIKE '%Business Mentoring %'); 

或者,你可能想這...

SELECT * 
    FROM default_schemes 
WHERE (region IN('Location1','Location2','Location3','Location4','Location5','Location6') 
    AND finance LIKE '%Equity%') 
    OR guidance LIKE '%Business Mentoring %'; 
0

的和運營商比或運營商更高的優先級,這樣的東西如A或B和C或D被解析爲A或(B和C)或D.

將括號添加到適當的位置以獲得th你想要的解釋。

0
SELECT * FROM (`default_schemes`) 
WHERE `region` IN ('Location1' 
, 'Location2' 
, 'Location3' 
, 'Location4' 
, 'Location5' 
, 'Location6') 
AND (`finance` LIKE '%Equity%' 
OR `guidance` LIKE '%Business Mentoring %') 

您能否請您檢查一下。