检索并格式化来自不同表

时间:2016-10-13 18:32:31

标签: mysql sql

tbl_marks:

+-------+-----------+-----------+-----------+-----------+----------+
|examid | section1  | section2  | section3  | section4  |    year  |
+-------+-----------+-----------+-----------+-----------+----------+ 
|E1     |       100 |      101  |      102  |      103  |    2016  |  
|E2     |       200 |      201  |      202  |      203  |    2015  |
|E3     |       300 |      301  |      302  |      303  |    2014  | 
|E4     |           |           |           |           |    2013  |
|E5     |      400  |      401  |      402  |      403  |    2016  |
|E6     |      500  |      501  |      502  |      503  |    2015  |
|E7     |      600  |      601  |      602  |      603  |    2014  |
|E8     |           |      701  |           |      703  |    2013  |
+-------+-----------+-----------+-----------+-----------+----------+ 

tbl_student:

+-------+-----+---------+
|name   | sid | rollnum |
+-------+-----+---------+
|cheery |   1 |      X1 |
|apple  |   2 |      X2 |
+-------+-----+---------+

tbl_exam:

+--------+------+
|examnum |  sid |
+--------+------+
|     E1 |    1 |
|     E2 |    1 |
|     E3 |    1 |
|     E4 |    1 |
|     E5 |    2 |
|     E6 |    2 |
|     E7 |    2 |
|     E8 |    2 |
+--------+------+

预期产出:

第1,2,3,4节代表考试第1,2,3,4部分的分数。 请注意,如果某个部分的分数为空,则应将其替换为零(000)。

最终输出表有列 - 得分,它是特定年份所有部分得分的连接结果。得分1代表2016年所有部分得分的连锁结果。同样得分2 - 2015年,得分3 - 2014年等。

输出应该有两行5列的2行 - 卷数,2016年,2015年,2014年和2013年的连续部分分数。

+------+------------+-------------+-------------+-------------+
|  num |  score1    |  score2     |  score3     |   score4    |
+------+------------+-------------+-------------+-------------+
|   X1 |100101102103| 200201202203| 300301302303| 000000000000|
|   X2 |400401402403| 500501502503| 600601602603| 000702000703|
+------+------------+-------------+-------------+-------------+

任何建议都表示赞赏。谢谢。

1 个答案:

答案 0 :(得分:1)

第1步: 加入

SELECT *
FROM tbl_student t_s
 LEFT JOIN tbl_exam t_e ON t_e.sid = t_s.sid 
 LEFT JOIN tbl_marks t_m ON t_m.examid = t_e.examnum

第二步: 选择大小写/何时

    SELECT t_s.rollnum as "num",
        CASE 
          WHEN t_m.year = '2016' THEN CONCAT_WS('',t_m.section1, t_m.section2, t_m.section3, t_m.section4)
        END AS score1,
        CASE 
          WHEN t_m.year = '2015' THEN CONCAT_WS('',t_m.section1, t_m.section2, t_m.section3, t_m.section4)
        END AS score2,
        CASE 
          WHEN t_m.year = '2014' THEN CONCAT_WS('',t_m.section1, t_m.section2, t_m.section3, t_m.section4)
        END AS score3,
        CASE 
          WHEN t_m.year = '2013' THEN CONCAT_WS('',t_m.section1, t_m.section2, t_m.section3, t_m.section4)
        END AS score4
   FROM tbl_student t_s
     LEFT JOIN tbl_exam t_e ON t_e.sid = t_s.sid 
     LEFT JOIN tbl_marks t_m ON t_m.examid = t_e.examnum

我们正朝着正确的方向前进。

第3步: MAX& GROUP BY

我们需要将所有结果分组为rollnum,因此所有其他选定列都需要是聚合函数。

    SELECT t_s.rollnum as "num",
        MAX(CASE 
          WHEN t_m.year = '2016' THEN CONCAT_WS('',t_m.section1, t_m.section2, t_m.section3, t_m.section4)
        END AS) score1,
        MAX(CASE 
          WHEN t_m.year = '2015' THEN CONCAT_WS('',t_m.section1, t_m.section2, t_m.section3, t_m.section4)
        END) AS score2,
        MAX(CASE 
          WHEN t_m.year = '2014' THEN CONCAT_WS('',t_m.section1, t_m.section2, t_m.section3, t_m.section4)
        END) AS score3,
        MAX(CASE 
          WHEN t_m.year = '2013' THEN CONCAT_WS('',t_m.section1, t_m.section2, t_m.section3, t_m.section4)
        END) AS score4
   FROM tbl_student t_s
     LEFT JOIN tbl_exam t_e ON t_e.sid = t_s.sid 
     LEFT JOIN tbl_marks t_m ON t_m.examid = t_e.examnum
   GROUP BY num

最后一步:合并

    SELECT t_s.rollnum as "num",
        MAX(CASE 
          WHEN t_m.year = '2016' THEN CONCAT_WS('',COALESCE(t_m.section1,'000'), COALESCE(t_m.section2,'000'), COALESCE(t_m.section3,'000'), COALESCE(t_m.section4,'000'))
        END AS) score1,
        MAX(CASE 
          WHEN t_m.year = '2015' THEN CONCAT_WS('',COALESCE(t_m.section1,'000'), COALESCE(t_m.section2,'000'), COALESCE(t_m.section3,'000'), COALESCE(t_m.section4,'000'))
        END) AS score2,
        MAX(CASE 
          WHEN t_m.year = '2014' THEN CONCAT_WS('',COALESCE(t_m.section1,'000'), COALESCE(t_m.section2,'000'), COALESCE(t_m.section3,'000'), COALESCE(t_m.section4,'000'))
        END) AS score3,
        MAX(CASE 
          WHEN t_m.year = '2013' THEN CONCAT_WS('',COALESCE(t_m.section1,'000'), COALESCE(t_m.section2,'000'), COALESCE(t_m.section3,'000'), COALESCE(t_m.section4,'000'))
        END) AS score4
   FROM tbl_student t_s
     LEFT JOIN tbl_exam t_e ON t_e.sid = t_s.sid 
     LEFT JOIN tbl_marks t_m ON t_m.examid = t_e.examnum
   GROUP BY num

有了这个你应该得到预期的结果,我无法测试它所以我可能犯了一些错误,如果它不起作用则发表评论。