我试图列出所有任务与完成/完成的任务(提交中)的计数。问题是我还想显示没有用户完成的所有任务。此查询未列出 count = 0 (Null)。有没有办法做到这一点?

想要的结果:

Date       | title   | completed
2014-05-20 | Case  1 | 45
2014-05-24 | Case 10 | 11
2014-05-20 | Case  2 |  0

到目前为止我已经尝试过:
Select date, title, count(*) as completed
from users u, submissions s, task t
where u.userPK = s.user
and s.task= t.taskPK
group by taskPK
order by completed desc;

最佳答案

我认为您应该能够使用 LEFT JOIN 来实现这一点:

SELECT date, title, COUNT(u.userPK) completed FROM task t
LEFT JOIN submissions s ON s.task = t.taskPK
LEFT JOIN users u ON s.user = u.userPK
GROUP BY t.taskPK
ORDER BY completed;

关于mysql - 使用MySQL计算存在和不存在的数量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23923583/

10-12 22:53