基本上,我尝试从基于两个不同群组的SQL获取数据,t.type
必须等于single
且t.status
必须等于1
但至于t.org
我希望同时获得DUAL
和RA
,这是我试图无效的内容。
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'
我非常确定他们能够让这个查询正常工作,而不是在我脑海中
答案 0 :(得分:20)
AND
的{{3}}高于OR
,因此您现有的表达式目前评估为:
WHERE
(t.type = 'single' AND t.status='1' AND t.org = 'RA') OR t.org = 'DUAL'
要强制使用替代逻辑,需要包含明确的括号:
WHERE
t.type = 'single' AND t.status='1' AND (t.org = 'RA' OR t.org = 'DUAL')
但是,在这种情况下,可以使用MySQL的precedence运算符而不是OR
:
WHERE
t.type = 'single' AND t.status='1' AND t.org IN ('RA','DUAL')
答案 1 :(得分:4)
您可以使用IN条件:
WHERE
t.type = 'single' AND t.status = '1' AND t.org IN ('RA','DUAL')
或者您可以使用括号对条件进行分组:
WHERE
t.type = 'single' AND t.status = '1' AND (t.org = 'RA' OR t.org = 'DUAL')