[英]mySQL: how to go along 3 different tables, referring to different column in each table but using primary keys
我试图找出哪些学校的学生在2018年未完成考试。因此,我设置了3个表格: ExamInfo
, ExamEntry
和Students
。 我将尝试使用ExamInfo
表从“ Students
表中获取信息,显然,我只希望那些在2018年未完成考试的学生信息。注意:我正在寻找参加但未完成的学生。考试,通过此特定考试,您可以将已完成的考试视为通过的考试。
在ExamInfo
我具有以下列:
ExamInfo_Date --when exam took place, using to get year() condition
ExamInfo_ExamNo --unique student exam ID used to connect with other tables
ExamInfo_Completed --1 if completed, 0 if not.
...
在ExamEntry
我具有相关的列:
ExamEntry_ExamNo --connected to ExamInfo table
ExamEntry_StudentId --unique studentId used to connect to Students table
ExamEntry_Date -- this is same as ExamInfo_Date if any relevance.
...
在Students
我有以下几列:
Students_Id --this is related to ExamEntry_StudentId, PRIMARY KEY
Students_School --this is the school of which I wish to be my output.
...
我希望我的输出仅是列出所有在2018年未完成考试的学生的学校的列表。尽管我的问题是从ExamInfo表中获取要查找未完成考试的学校的信息。
到目前为止,我已经:
SELECT a.Students_School, YEAR(l.ExamInfo_Date), l.ExamInfo_Completed
FROM ExamInfo l ??JOIN?? Students a
WHERE YEAR(l.ExamInfo_Date) = 2018
AND l.ExamInfo_Completed = 0
;
我什至不确定是否需要通过ExamEntry
表。 我确定我打算使用联接,尽管不确定如何适当地使用它。 另外,对于我的3个不同的SELECT列,我只希望输出Students_School列:
Students_School
---------------
Applederry
Barnet Boys
...
显然,您需要一个JOIN
实际上是两个。 您的表中包含考试 , 学生和一个联结/关联表,该表表示这些实体之间的多对多关系。
因此,我希望FROM
子句看起来像:
FROM ExamInfo e JOIN
ExamEntry ee
ON ee.ExamEntry_ExamNo = e.ExamNo JOIN
Students s
ON ee.ExamEntry_StudentId = s.Students_Id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.