基本上,我试图从基于两个不同组的 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'
我很确定他们有办法让这个查询正常工作,只是不在我的脑海里
最佳答案
AND
具有更高的 precedence比 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 的 IN()
运算符而不是 OR
:
WHERE
t.type = 'single' AND t.status='1' AND t.org IN ('RA','DUAL')
关于mysql - OR WHERE 和 AND 在 mySQL 查询中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17750098/