我有这个问题:
SELECT
pupils.id_pupil
, name
, surname
, (select round(avg(qualifications.calificacion),2) from qualifications where id_pupil = pupils.id_pupil and qualifications.id_trimester=incidents.id_trimester and type_qualification='class') as media
, (select count(calificacion) from qualifications where id_pupil = pupils.id_pupil and qualifications.id_trimester=incidents.id_trimester and type_qualification='class') as count
, COALESCE(sum(type_incident='miss' and level=1 and class='A' and id_trimester=1),0) as misses
, COALESCE(sum(type_incident='delay' and level=1 and class='A' and id_trimester=1),0) as delays
, COALESCE(sum(type_incident='attitude' and level=1 and class='A' and id_trimester=1),0) as attitude
, COALESCE(sum(type_incident='miss_justif' and level=1 and class='A' and id_trimester=1),0) as misses_justificadas
FROM
pupils
left join incidents ON incidents.id_pupil=pupils.id_pupil
WHERE
level=1
and class='A'
and id_trimester=1
GROUP BY id_pupil
表:
pupils: (id_pupil, name, surname, email, user, pass, level, class)
incidents: (id_incidents, date, time, type_incident, comments, id_pupil, id_user, subject, id_trimester)
qualifications: (id_qualification, qualification, date, time, subject, id_pupil, id_user, id_trimester, type_qualification)
它有效,但它没有显示带有未命中,延迟,态度和misses_justif的行,其中四个值为0.该行被省略。
如何显示所有行?
谢谢!
答案 0 :(得分:0)
您的查询存在一些问题。我做了一个修改,也许你给了你想要的结果。也许你应该把名字和姓氏,媒体和计数值放在你的组中,但这取决于你,因为它取决于你的要求。
SELECT
pupils.id_pupil
, name
, surname
, IFNULL(qua.media, 0) media
, IFNULL(qua.count, 0) count
, sum( case when type_incident='miss' then 1 else 0 end ) as misses
, sum( case when type_incident='delay' then 1 else 0 end ) as delays
, sum( case when type_incident='attitude' then 1 else 0 end ) as attitude
, sum( case when type_incident='miss_justif' then 1 else 0 end )
as misses_justificadas
FROM
pupils
LEFT JOIN incidents ON incidents.id_pupil=pupils.id_pupil
LEFT JOIN
(select id_pupil, id_trimester,
count(qualification) count,
round(avg(qualifications.qualification),2) media
from qualifications
where type_qualification='class'
group by id_pupil, id_trimester ) as qua
ON ( qua.id_pupil = pupils.id_pupil )
WHERE
level=1
and class='A'
and qua.id_trimester=1
GROUP BY pupils.id_pupil