SQL没有返回预期的行

时间:2014-06-01 20:27:29

标签: mysql sql

我的SQL SELECT语句未返回正确的行。我有一个名为activities的表,该语句应该只输出列'approved'= yes的行,其中一些单元格有一些文本LIKE'test'。但我的陈述仍在输出批准=否的一些行,但不是全部。

这是我的sql语句:

SELECT * FROM activities WHERE approved = 'yes' AND title LIKE '%test%' OR subject LIKE '%test%' OR industry LIKE '%test%' OR description LIKE '%test%' ORDER BY date_added DESC

1 个答案:

答案 0 :(得分:2)

()内的所有OR条件移动为

SELECT * FROM activities
WHERE 
approved = 'yes' 
AND ( 
title LIKE '%test%' 
OR subject LIKE '%test%'
OR industry LIKE '%test%' 
OR description LIKE '%test%' 
)
ORDER BY date_added DESC

所以它会将过滤器作为

  • 如果获得批准='是'
  • 和标题一样的东西或主题像......