2013-05-05 69 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