课程和学生成绩表 - MySQL

时间:2016-02-09 00:43:05

标签: php mysql

我想制作课程和学生分数表。

吸取的;

ID   | Lesson_Name | Teacher_Name
---------------------------------
1    | Maths       |   Teacher1
2    | Literature  |   Teacher2
3    | Psychology  |   Teacher3
4    | Law         |   Teacher4

Student_Marks;

ID   | LessonID | Student ID | Mark
---------------------------------
1    | 1        |   1        | 100
2    | 1        |   2        | 70
3    | 2        |   1        | 80
4    | 3        |   2        | 30
5    | 2        |   3        | 60

生;

ID   | Student
---------------------------------
1    | John
2    | Ashley
3    | Geroge
4    | Steve

我想要一张像这样的桌子;

Lesson      | Teacher  | Student1 | Student2 | Student3
-------------------------------------------------------
Maths       | Teacher1 |  100     | 70       | 60   
Literature  | Teacher2 |  70      | 40       | 
Psychology  | Teacher3 |          | 50       | 30 
Law         | Teacher4 |  30      | 60       | 

目前,我使用下面的查询获取所有数据并使用PHP解析它们以构建表。

SELECT
    l.id,l.lesson_id,l.lesson_name,l.teacher_name,
    GROUP_CONCAT(CONCAT_WS(':', sm.studentid, sm.studentmark) SEPARATOR ',') as students
FROM lessons l
LEFT JOIN student_marks sm ON l.lesson_id = sm.lesson_id
GROUP BY l.lesson_id

有更有效的方法吗?

0 个答案:

没有答案