我一直致力于管理当地高中橄榄球联盟的访问项目......
我有2张桌子: tbl_schools,包含字段:
[id_school, school_name]
和 tbl_penalties,包含字段:
[id_penalty, school, player, match, card]
和查询,query_penalties:
SELECT DISTINCTROW tbl_penalties.school, Count(tbl_penalties.school) AS penalties
FROM tbl_penalties
GROUP BY tbl_penalties.school;
任务/我的问题: 我需要一个查询来显示(在它的第一列)tbl_schools中的所有学校和(在它的第二列)学校受到惩罚的次数,如果学校没有受到任何惩罚,则需要0 ...
答案 0 :(得分:1)
SELECT S.school_name
,COUNT(P.school) AS penalties
FROM tbl_schools S LEFT JOIN tbl_penalties P
ON S.id_school = P.school
GROUP BY S.school_name