我还是数据库和 mysql 的新手,我想学习如何使用JOINs
。对不起,我只是不知道用文字表达这个案例。我希望大家通过查看这些数据能够理解。这是带有记录的表:
Table student
student_id | student_name
-------------------------
1 Ana
2 Billy
3 Connor
Table comp
comp_id | subj_id | comp_name
--------------------------
1 24 Run
2 24 Swim
3 24 Jump
4 25 Eat
Table comp_mark
semester | subj_id | student_id | comp_id | mark
-------------------------------------------------
1 24 1 1 7
1 24 1 2 4
1 24 1 3 6
1 24 2 1 4
1 24 2 2 8
1 24 3 1 9
我期待查询选择comp_mark
表(更新)的结果是这样的:
student_name | semester | subject_id | comp_id | mark
-------------------------------------------------------
Connor 1 24 1 9
Connor null 24 2 null
Connor null 24 3 null
有人可以帮我吗?非常感谢
更新
我已经尝试过(Guillaume Poussell 的查询)并按 student_name 对其进行排序:
SELECT s.student_name, cm.semester, c.subj_id AS subject_id, c.comp_id, cm.mark
FROM student s
CROSS JOIN comp c
LEFT JOIN comp_mark cm ON s.student_id = cm.student_id
AND cm.subj_id = c.subj_id
AND cm.comp_id = c.comp_id
ORDER BY s.student_name
结果: