我在寻找这个小问题的解决方案时遇到了一些麻烦。我想得到团队的数量,我也需要零计数。如果我不提及特定的团队,这是有效的。但是当查询变得复杂时无法工作。
SELECT teams.name, COUNT(checkins.team_id) AS "count of checkins"
FROM checkins
RIGHT JOIN teams ON checkins.team_id = teams.id
GROUP BY checkins.team_id
这样修改后就失效了
SELECT teams.name, COUNT(checkins.team_id) AS "count of checkins"
FROM checkins
RIGHT JOIN teams ON checkins.team_id = teams.id
WHERE checkins.team_id IN (1,3,2)
GROUP BY checkins.team_id
最佳答案
你可以试试这个:
SELECT teams.name, COUNT(checkins.team_id) AS "count of checkins"
FROM checkins
RIGHT JOIN teams ON checkins.team_id = teams.id
WHERE (checkins.team_id IN (1,3,2) OR checkins.team_id IS NULL )
GROUP BY checkins.team_id
关于mysql - SQL 查询不工作。 Count 也应该返回零。但即使在外部加入后也不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45417066/