OR WHERE和AND在MySQL查询中 [英] OR WHERE and AND in mySQL Query

查看:126
本文介绍了OR WHERE和AND在MySQL查询中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在尝试从基于两个不同组的SQL获取数据,t.type必须等于single,而t.status必须等于1,但是对于t.org我想要同时得到DUALRA,这是我尝试的尝试.

Basically, I'm trying to get data from an SQL based on two different groups, t.type has to equal single and t.status has to equal 1 but as for t.org I want to it get both DUAL and RA, here's what I attempted to no avail.

SELECT 
    COUNT( p.tID ) 
FROM 
    ticket AS t 
INNER JOIN 
    people AS p ON t.ID = p.tID 
WHERE 
    t.type = 'single' AND t.status='1' AND t.org = 'RA' OR t.org = 'DUAL'

我很确定他们的方法可以使此查询正常运行,只是不在我的脑海

I'm pretty sure theirs a way to get this query working, just not in my head

推荐答案

AND具有更高的

AND has higher precedence than OR, so your existing expression is currently evaluated as:

WHERE 
    (t.type = 'single' AND t.status='1' AND t.org = 'RA') OR t.org = 'DUAL'

要强制采用替代逻辑,需要包含显式括号:

To force alternative logic, one needs to include explicit parentheses:

WHERE 
    t.type = 'single' AND t.status='1' AND (t.org = 'RA' OR t.org = 'DUAL')

但是,在这种情况下,可以使用MySQL的 IN() 运算符,而不是OR:

However, in this case, one can use MySQL's IN() operator instead of OR:

WHERE 
    t.type = 'single' AND t.status='1' AND t.org IN ('RA','DUAL')

这篇关于OR WHERE和AND在MySQL查询中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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