我在MYSQL中有4个表,我想显示所有36个区的learner_history表的状态列区域的计数,但是当我查询它时只显示1个区域结果,即使我尝试了左连接以显示区域名称天气有数据是否可用它必须在其位置显示null或零。对于所有地区,数据应如下所示:
数据库结构是这样的 [在此处输入图像描述] [我在此图像中绘制表格的关系] 请建议哪些查询会给出这样的结果,谢谢。
答案 0 :(得分:1)
使用图像进行工作非常尴尬,但它应该能满足您的要求。还不确定你想要什么'Str。#' - 你用22给出的示例图像,因为每个结果都不是最有帮助的。
select
District.District_Name as District,
sum(Learner_History.`Status` = 'catchment') as Catchment,
sum(Learner_History.`Status` = 'fresher') as Fresher,
sum(Learner_History.`Status` = 'dropout') as Dropout,
sum(Learner_History.`Status` = 'missout') as Missout,
sum(Learner_History.`Status` = 'mainstrain') as Mainstrain,
sum(Learner_History.`Status` = 'pec') as PEC
from District
left join School on School.District_ID = District.District_ID
left join Learner on Learner.School_ID = School.School_ID
left join Learner_History on Learner_History.Learner_ID = Learner.Learner_ID
group by District.District_Name;