2013-05-05 68 views
0

我正在創建搜索某些圖片的搜索功能。每張照片都有一個狀態,表示它是否被批准或拒絕。 mysql在返回之前檢查狀態,但它仍然返回圖像,不應該返回。MySQL使用LIKE,AND和OR

這裏是我的查詢:

SELECT * FROM Pictures 
WHERE ImageTitle LIKE '%yaa%' 
OR ImageDescription LIKE '%yaa%' 
AND Approval='Approved' 
Order BY DateTime DESC 

「YAA」 是對於現在的搜索。這只是一個例子。 查詢返回標記爲已批准的結果,但也標記爲已拒絕的結果。我的查詢出了什麼問題?

我試着將AND語句移動到查詢的開頭,返回相同的結果。

回答

5

用括號將OR條件分組。

SELECT * 
FROM Pictures 
WHERE (ImageTitle LIKE '%yaa%' 
     OR ImageDescription LIKE '%yaa%') 
     AND Approval='Approved' 
Order BY DateTime DESC 
+1

謝謝你的快速反應,是工作! :D – Jazerix 2013-05-05 11:41:52

+0

不客氣':)' – 2013-05-05 11:46:02

1
/*There should be a parenthesis for 'or' condition, and the state condition should remain outside of the parenthesis*/ 
SELECT * FROM Pictures 
WHERE (ImageTitle LIKE '%yaa%' 
OR ImageDescription LIKE '%yaa%') 
AND Approval='Approved' 
Order BY DateTime DESC