-1

I have a little problem. This query gives also records where the field 'active' is 0. I tought the problem was something with the brackets, but i can't find out how it should be.

Anyone an idea?

SELECT id, title, message_short, date_posted, posterID, active, deleted 
FROM articles
WHERE title LIKE '%blah%' OR
      message LIKE '%blah%' AND
      deleted = 0 AND
      active = 1
ORDER BY date_posted DESC
Gordon Linoff
  • 1,198,228
  • 53
  • 572
  • 709
  • Use parentheses. – Gordon Linoff Apr 14 '18 at 22:38
  • 1
    To explain why: Read out what your query's where clause was saying. You will see the ambiguity in the meaning since you have no brackets. You need to understand the [precedence of the operators](https://stackoverflow.com/questions/1241142/sql-logic-operator-precedence-and-and-or). P.S. regarding the title "Query gives not the right records": computers always do what you tell them... even if that isn't what you intended. So it actually did give the right records. :) – Richardissimo Apr 14 '18 at 22:43
  • I understand! :-) – Jack Vonderberg Apr 14 '18 at 23:21

1 Answers1

1

try this

 SELECT id, title,message_short,date_posted,posterID,active,deleted FROM 
 articles WHERE (title LIKE '%blah%' OR message LIKE '%blah%')
 AND deleted=0 AND active=1 ORDER BY date_posted DESC
Kiran
  • 3,220
  • 3
  • 13
  • 20