如何使用链接表连接表而不使用公共列

时间:2013-10-08 12:23:46

标签: database foreign-keys entity-relationship relationship

我们如何加入这些student_info和class_info表,没有公共列,但两个外键都存储在class_student表中。

1 个答案:

答案 0 :(得分:0)

调整以适合您的WHERE子句,特定数据库和JOIN需求。

对于学生的课程:

SELECT *
FROM student_info JOIN class_student ON student_info.student_id = class_student.student_id
                  JOIN class_info    ON class_student.class_id  = class_info.class_id
WHERE student_info.student_id = ...

对于班级的学生:

 SELECT *
 FROM class_info JOIN class_student ON class_info.class_id      = class_student.class_id
                 JOIN student_info  ON class_student.student_id = student_info.student_id
 WHERE class_info.class_id = ...