MySQL Query with multiple AND statements seems to be ignoring one

后端 未结 5 1510
花落未央
花落未央 2020-12-20 00:25

I\'m trying to run a query on a MySQL database, but I found that it seems to be ignoring the \'status\' item.

SELECT * FROM  `posts`  
WHERE
      `tags` LIK         


        
5条回答
  •  不知归路
    2020-12-20 01:23

    Usually mixing ORs and ANDs is not a smart practice, since you'll get different results based on the execution order and the operator precedence, e.g, true or true and false, can be evaluated as (true or true) and false - yielding false or as true or (true and false) - yielding true.

    Use parentheses to separate the ORs and the ANDs, and the execution order will be explicit, like this:

    SELECT * FROM  `posts`
    WHERE (`tags` LIKE '%gda%' OR `tags` LIKE '%contests%' OR `tags` LIKE '%merch%')
      AND `newsID` != '2134'
      AND `status` > '1'
    ORDER BY  `postDate` DESC  LIMIT 5 
    

提交回复
热议问题