首页 > 解决方案 > 如何从三个表中提取行

问题描述

我知道问这个问题是不合适的。

学生表 - ID、姓名、GPA

班级表 - ID、职务、学期

Student_Class 表 - Student_ID、Class_ID、Student_Grade

从这个表结构中,如何提取参加 class.title = 'MATH101' 和 class.semester = 'FALL2018' 的学生姓名。

没有太多研究,我不得不问这个问题。我怎样才能开始呢?

标签: sql

解决方案


Student_ClassStudent是连接表,它实现和之间的多对多关系Class。所以它只需要加入Student_Classwith Studentonstudent_idStudent_Classwith Classon class_id

select s.name
from Student s inner join Student_Class cs on s.id = cs.student_id
inner join Class c on cs.class_id = c.id
where c.title = ‘MATH101’ and c.semester = ‘FALL2018’.

推荐阅读