具有多个AND语句的MySQL查询似乎忽略了一个 [英] MySQL Query with multiple AND statements seems to be ignoring one

查看:136
本文介绍了具有多个AND语句的MySQL查询似乎忽略了一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在MySQL数据库上运行查询,但是我发现它似乎忽略了'status'项目.

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` LIKE '%gda%' 
   OR `tags` LIKE '%contests%' 
   OR `tags` LIKE '%merch%' 
  AND `newsID` != '2134' 
  AND `status` > '1' 
ORDER BY  `postDate` DESC  LIMIT 5

在该示例中,即使将状态"设置为0,它仍在提取项目.我在做什么错了?

In that example, it's still pulling items even if 'status' is set to 0. What am I doing wrong?

推荐答案

问题在于OR/AND条件的优先级. AND的优先级高于OR,这就是为什么它首先评估由AND连接的所有条件(tags-merch,newsID-2134和status-1),然后评估tags-gda和tags-contests的原因

The problem is with the priority of OR/AND conditions. AND has a higher priority than OR, that's why it firstly evaluates all conditions connected by AND (tags-merch, newsID-2134 and status-1) and then evaluates the both tags-gda and tags-contests).

尝试添加括号:

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

这篇关于具有多个AND语句的MySQL查询似乎忽略了一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆