mysql ) 如何使用分组依据进行打印,包括当计数为 0 时

mysql ) How to print using group by, including when count is 0

当我有这样一个table,

我想获得如下图所示的 select 语句的结果。

但是,当计数为 0 时,该行将被省略。

这让我很困惑。 我如何修改此 SQL 语句以打印 count = 0 行?

SELECT ca.syear, COUNT(cc.joined),cc.joined
FROM schedules AS cc JOIN
     schedule_assignment AS ca
     ON ca.idx = cc.assignment
where student = 1 AND  cc.joined = 1
GROUP BY ca.syear;

抱歉,我的声望低于 10,所以我无法立即显示图片。 :(

我想你只是想要一个 LEFT JOIN:

SELECT sa.syear, COUNT(cc.joined)
FROM schedule_assignment sa LEFT JOIN
     schedules s 
     ON ca.idx = cc.assignment AND
        s.student = 1 AND 
        s..joined = 1
GROUP BY sa.syear;

我没有在结果集中包含 joined 它总是 1,所以这似乎没有用。你可以包括它:1 as joined,如果你真的想要它。