所以我有一个查询似乎工作正常,直到这一行WHERE s.status = 'pending'
它返回数据'closed'
。此数据部分只有3个选项可供挂起,已关闭和生效。
我不知道为什么会这样做,我使用相同的查询,但在页面的不同部分有status = 'closed'
(在此查询下面,因此变量不应该是冲突的),那么为什么这样做呢?现在的方式?
SELECT s.gameid, s.gameday, s.hometeam AS hid, ht.teamname AS homename, s.awayteam AS aid, at.teamname AS awayname, s.status
FROM schedule s
INNER JOIN teams ht ON s.hometeam = ht.teamid
INNER JOIN teams at ON s.awayteam = at.teamid
WHERE s.status = 'pending' AND s.hometeam = $tid OR s.awayteam = $tid ORDER BY s.gameday ASC LIMIT 5
答案 0 :(得分:6)
将OR
d部分括在括号内的WHERE
子句中。
WHERE s.status = 'pending' AND (s.hometeam = $tid OR s.awayteam = $tid)
ORDER BY s.gameday ASC LIMIT 5
您的原始查询是有效的:
WHERE (s.status = 'pending' AND s.hometeam = $tid) OR s.awayteam = $tid
这显然是不正确的。如果您查看MYSQL's operator precedence,AND
优先于OR
。