简体   繁体   English

mySQL:如何遍历3个不同的表,引用每个表中的不同列,但使用主键

[英]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个表格: ExamInfoExamEntryStudents 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.

相关问题 如何在2个不同的表中强制使用不同的主键? - How to enforce different primary keys in 2 different tables? 从mysql中的不同表中获取2个主键 - To get 2 primary keys from different tables in mysql MYSQL使用来自不同表的外键将数据插入表 - MYSQL Insert Data into table using Foreign Keys from different tables 使用引用不同表的触发器 - Using triggers referring to different tables 我可以从 2 个不同表的主键为第三个表创建复合键吗? MySQL支持吗? - Can I create a composite key for a third table from the primary keys of 2 different tables? Does MySQL support it? 如何对列名与其主键相同但值不同的表执行 UNION - How to perform UNION of the tables with same column name as their primary keys but different values 为mysql表中的每个主键和外键使用唯一的列名,以避免产生歧义 - Using of unique column name for each primary and foreign keys in mysql table to avoid ambiguous condition 如何将具有两个不同主键的两个表联接到另一个表中? - How can I join two tables with two different primary keys into another table? 如何使用mysql连接具有相同属性但每个表中数据不同的2个表 - how to join 2 tables having equal attributes but different data in each table using mysql MySQL-如何列出所有具有外键的表,这些外键都指向表的主键? - MySQL - How to list all tables that have a foreign key referring to my table's primary key?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM