我有这些表:
我要达到的目标:我希望学生识别号出现一次,并且每个科目分数都显示在该科目下方,就像下面的图片一样。如何在我的SQL查询中解决这个问题?
我的源代码:
$sql = "SELECT distinct s.regiNo, s.firstName as fname, s.middleName as mname, s.lastName as lname, s.gender, s.class_group, c.subjects, e.First_CA, e.Second_CA, e.examid, e.scored, e.internaltype, e.Class, e.Year FROM student s, subjects c, exam e WHERE s.regiNo = e.Roll_Number AND e.sub_id = c.subect_code
";
谢谢。
答案 0 :(得分:1)
您似乎想要有条件的聚合:
select s.regiNo as IdentityNumber, e.Class, s.class_group,
sum(case when c.subect_code = 'math' then (e.First_CA + e.Second_CA + e.scored) else 0 end) as Mathematics,
. . .
from student s inner join
exam e
on s.regiNo = e.Roll_Number inner join
subjects c
on e.sub_id = c.subect_code
group by s.regiNo, e.Class, s.class_group;