[英]mySQL: how to go along 3 different tables, referring to different column in each table but using primary keys
I'm trying to find out which schools had students that did not complete their exams in 2018. So I've got 3 tables set up being: ExamInfo
, ExamEntry
and Students
. 我试图找出哪些学校的学生在2018年未完成考试。因此,我设置了3个表格: ExamInfo
, ExamEntry
和Students
。 I'm going to try use the ExamInfo
table to get information from the Students
table though, I obviously only want the student information that did not complete their exam in 2018. Note: I'm looking for students that attended, though did not complete the exam, with this particular exam you can look at completed exam as passed exam. 我将尝试使用ExamInfo
表从“ Students
表中获取信息,显然,我只希望那些在2018年未完成考试的学生信息。注意:我正在寻找参加但未完成的学生。考试,通过此特定考试,您可以将已完成的考试视为通过的考试。
Within ExamInfo
I have the columns: 在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.
...
Within ExamEntry
I have the related columns: 在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.
...
Within Students
I have following columns: 在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.
...
I want my output to simply be a list of all schools that had students that did not complete their exams in 2018. Though my issue is with getting from the ExamInfo table, to finding the schools of which students did not complete their exam. 我希望我的输出仅是列出所有在2018年未完成考试的学生的学校的列表。尽管我的问题是从ExamInfo表中获取要查找未完成考试的学校的信息。
So far I've: 到目前为止,我已经:
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
;
I'm not even sure if going through the ExamEntry
table is necessary. 我什至不确定是否需要通过ExamEntry
表。 I'm sure I'm meant to use a join, though unsure of how to appropriately use it. 我确定我打算使用联接,尽管不确定如何适当地使用它。 Also, with my 3 different SELECT columns, I only wish for Students_School column to be output: 另外,对于我的3个不同的SELECT列,我只希望输出Students_School列:
Students_School
---------------
Applederry
Barnet Boys
...
Clearly, you need a JOIN
-- two in fact. 显然,您需要一个JOIN
实际上是两个。 Your table has exams , students , and a junction/association table that represents the many-to-many relationship between these entities. 您的表中包含考试 , 学生和一个联结/关联表,该表表示这些实体之间的多对多关系。
So, I would expect the FROM
clause to look like: 因此,我希望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.