Count(*)允许group by null

时间:2012-06-17 21:55:09

标签: mysql sql

我有以下查询:

select count(*) as memo_count, student_id from memo group by student_id

返回以下结果集:

memo_count        student_id
3                 0
8                 1

然而,我真正希望它返回的是:

memo_count        student_id
3                 0
8                 1
0                 2

发生的事情是,Group By过滤掉任何返回0的计数(*),这不是我想要的。有什么方法吗?感谢。

1 个答案:

答案 0 :(得分:2)

你可以尝试这样的事情。假设你有一张学生表......

Select count(m.id), s.id from student s 
 left outer join memo m on m.student_id = s.id 
 group by s.id