如何在MySQL的每个主题下放置主题分数

时间:2018-07-02 11:39:44

标签: php mysql sql

我有这些表:

显示从数据库中获取结果后的结果的表: enter image description here

数据库的结果表: enter image description here

数据库中的主题表: enter image description here

我要达到的目标:我希望学生识别号出现一次,并且每个科目分数都显示在该科目下方,就像下面的图片一样。如何在我的SQL查询中解决这个问题?

enter image description here

我的源代码:

$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
";

谢谢。

1 个答案:

答案 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;