在单个MYSQL表中汇总多个列

时间:2016-05-26 10:39:59

标签: mysql

+---------+------------+------------+-----------------------+-----------------+---------------------+
| exam_id | student_id | subject_id | assignment_mark_total | test_mark_total | exams_mark_obtained |
+---------+------------+------------+-----------------------+-----------------+---------------------+
|       1 |         19 |          1 |                    20 |              30 |                  50 |
|       1 |         20 |          1 |                    18 |              27 |                  48 |
|       1 |         21 |          1 |                    16 |              27 |                  49 |
|       1 |         22 |          1 |                    16 |              29 |                  47 |
|       1 |         23 |          1 |                     0 |               0 |                   0 |
|       1 |         24 |          1 |                     0 |               0 |                   0 |
|       1 |         25 |          1 |                     0 |               0 |                   0 |
|       1 |         26 |          1 |                     0 |               0 |                   0 |
|       1 |         27 |          1 |                     0 |               0 |                   0 |
|       1 |         28 |          1 |                     0 |               0 |                   0 |
|       1 |         29 |          1 |                     0 |               0 |                   0 |
|       1 |         30 |          1 |                     0 |               0 |                   0 |
|       1 |         31 |          1 |                     0 |               0 |                   0 |
|       1 |         32 |          1 |                     0 |               0 |                   0 |
|       1 |         33 |          1 |                     0 |               0 |                   0 |
|       1 |         34 |          1 |                     0 |               0 |                   0 |
|       1 |         35 |          1 |                     0 |               0 |                   0 |
|       1 |         36 |          1 |                     0 |               0 |                   0 |
|       1 |         37 |          1 |                     0 |               0 |                   0 |
|       1 |         38 |          1 |                     0 |               0 |                   0 |
|       1 |         39 |          1 |                     0 |               0 |                   0 |
|       2 |         19 |          1 |                    20 |              21 |                  49 |
|       2 |         20 |          1 |                    20 |              21 |                  50 |
|       2 |         21 |          1 |                    16 |              27 |                  46 |
|       2 |         22 |          1 |                     0 |               0 |                   0 |
|       2 |         23 |          1 |                     0 |               0 |                   0 |
|       2 |         24 |          1 |                     0 |               0 |                   0 |
|       2 |         25 |          1 |                     0 |               0 |                   0 |
|       2 |         26 |          1 |                     0 |               0 |                   0 |
|       2 |         27 |          1 |                     0 |               0 |                   0 |
|       2 |         28 |          1 |                     0 |               0 |                   0 |
|       2 |         29 |          1 |                     0 |               0 |                   0 |
|       2 |         30 |          1 |                     0 |               0 |                   0 |
|       2 |         31 |          1 |                     0 |               0 |                   0 |
|       2 |         32 |          1 |                     0 |               0 |                   0 |
|       2 |         33 |          1 |                     0 |               0 |                   0 |
|       2 |         34 |          1 |                     0 |               0 |                   0 |
|       2 |         35 |          1 |                     0 |               0 |                   0 |
|       2 |         36 |          1 |                     0 |               0 |                   0 |
|       2 |         37 |          1 |                     0 |               0 |                   0 |
|       2 |         38 |          1 |                     0 |               0 |                   0 |
|       2 |         39 |          1 |                     0 |               0 |                   0 |
|       3 |         19 |          1 |                     0 |              27 |                  36 |
|       3 |         20 |          1 |                     0 |              24 |                  41 |
|       3 |         21 |          1 |                     0 |              25 |                  48 |
|       3 |         22 |          1 |                     0 |               0 |                   0 |
|       3 |         23 |          1 |                     0 |               0 |                   0 |
|       3 |         24 |          1 |                     0 |               0 |                   0 |
|       3 |         25 |          1 |                     0 |               0 |                   0 |
|       3 |         26 |          1 |                     0 |               0 |                   0 |
|       3 |         27 |          1 |                     0 |               0 |                   0 |
|       3 |         28 |          1 |                     0 |               0 |                   0 |
|       3 |         29 |          1 |                     0 |               0 |                   0 |
|       3 |         30 |          1 |                     0 |               0 |                   0 |
|       3 |         31 |          1 |                     0 |               0 |                   0 |
|       3 |         32 |          1 |                     0 |               0 |                   0 |
|       3 |         33 |          1 |                     0 |               0 |                   0 |
|       3 |         34 |          1 |                     0 |               0 |                   0 |
|       3 |         35 |          1 |                     0 |               0 |                   0 |
|       3 |         36 |          1 |                     0 |               0 |                   0 |
|       3 |         37 |          1 |                     0 |               0 |                   0 |
|       3 |         38 |          1 |                     0 |               0 |                   0 |
|       3 |         39 |          1 |                     0 |               0 |                   0 |
+---------+------------+------------+-----------------------+-----------------+---------------------+
63 rows in set (0.00 sec)

您好,我有一个学校报告表的示例数据库,我希望能够得到一个数组结果,其中包含3个学期的单个科目的总和

我希望得到这样的结果,作为(assignment_mark_total + test_mark_total + exams_mark_obtained) = 1& 2& 3

每个主题的exam_id AS分数的总和
+--------+
| scores |
+--------+
|    253 |
|    249 |
|    254 |
|     92 |
|      0 |
|      0 |
|      0 |
|      0 |
|      0 |
|      0 |
|      0 |
|      0 |
|      0 |
|      0 |
|      0 |
|      0 |
|      0 |
|      0 |
|      0 |
|      0 |
|      0 |
+--------+

2 个答案:

答案 0 :(得分:0)

我假设你正在为每个学生寻找3个考试的总分。试试这个问题:

SELECT
    SUM(assignment_mark_total + test_mark_total + exams_mark_obtained) AS scores
FROM
    table_name
WHERE
    exam_id in (1, 2, 3)
GROUP BY 
    student_id

您可能还想在所选列列表中添加 student_id

答案 1 :(得分:0)

尝试查询。我想这可能会对你有所帮助。

select sum(assignment_mark_total + test_mark_total + exams_mark_obtained) as  scores
        where exam_id = 1 or exam_id = 2 or exam_id = 3
            group by student_id;

如果要使用student_id的值对结果进行排序,请按照此查询

select sum(assignment_mark_total + test_mark_total + exams_mark_obtained) as  scores
        where exam_id = 1 or exam_id = 2 or exam_id = 3
            group by student_id
                  order by student_id asc;