简体   繁体   English

MySQL-连接多个表

[英]mySQL - joining multiple tables

I have tables shown below. 我有下表所示。 I am using this for my android quiz application. 我正在将其用于我的android测验应用程序。 I want to fetch the students who cant take the quiz. 我想拿那些不能参加测验的学生。

Table quiz 表测验

+-------------------------------------------------------------------+
| id | title  |   start    |    end     | class_name | user_faculty |
+-------------------------------------------------------------------+
| 1  | Quiz 1 | 2016-08-01 | 2016-08-03 | T4         | faculty_1    |
+-------------------------------------------------------------------+
| 2  | Quiz 2 | 2016-08-01 | 2016-08-03 | T4         | faculty_1    |
+-------------------------------------------------------------------+
| 3  | Quiz 3 | 2016-08-03 | 2016-08-04 | T4         | faculty_1    |
+-------------------------------------------------------------------+

Table studentclass 表学生班

+-----------------------------------------------+
| id | class_name | user_faculty | user_student |
+-----------------------------------------------+
| 1  | T4         | faculty_1    | student_1    |
+-----------------------------------------------+
| 2  | T4         | faculty_1    | student_2    |
+-----------------------------------------------+

My initial query here is: 我最初的查询是:

SELECT Q.id, Q.title, Q.start, Q.end
FROM quiz Q
INNER JOIN studentclass SC
ON Q.class_name = SC.class_name
AND Q.start_on <= now() 
AND Q.end_on >= now()
AND SC.user_student = 'student_1'

consider that the date today is 2016-08-02 考虑今天是2016年8月2日

I am able to fetch the first and second row of the quiz table 我能够获取测验表的第一行和第二行

+---------------------------------------+
| id | title  |   start    |    end     |
+---------------------------------------+
| 1  | Quiz 1 | 2016-08-01 | 2016-08-03 |
+---------------------------------------+
| 2  | Quiz 2 | 2016-08-01 | 2016-08-03 |
+---------------------------------------+

Now I also want to fetch the score table together with this previous query. 现在,我也想与之前的查询一起获取分数表。 A student 'student_1' finished taking the quiz. 学生“ student_1”完成了测验。

Table score 表分数

+--------------------------------------------------------+
| id | score | grade | user_student | quiz_id | status   |
+--------------------------------------------------------+
| 1  | 10    | 100   | student_1    | 1       | Finished |
+--------------------------------------------------------+

My new query 我的新查询

SELECT Q.id, Q.title, Q.start, Q.end, S.user_student, S.status
FROM quiz Q
INNER JOIN studentclass SC
ON Q.class_name = SC.class_name
AND Q.start_on <= now() 
AND Q.end_on >= now()
AND SC.user_student = 'student_1'
LEFT JOIN score S
ON Q.id = S.quiz_id
WHERE S.quiz_id IS NULL

the result is 结果是

+---------------------------------------------------------------+
| id | title  |   start    |    end     | user_student | status |
+---------------------------------------------------------------+
| 2  | Quiz 2 | 2016-08-01 | 2016-08-03 | NULL         | NULL   |
+---------------------------------------------------------------+

the result is going fine but when I change the student_1 to student_2 in my 2nd query I am still getting the same result. 结果很好,但是当我在第二个查询中将student_1更改为Student_2时,我仍然得到相同的结果。 The student_2 should see both of the quiz rows because he haven't taken the quiz yet. 学生_2应该看到两个测验行,因为他还没有参加测验。

SELECT Q.id, Q.title, Q.start, Q.end, S.user_student, S.status
FROM quiz Q
INNER JOIN studentclass SC
ON Q.class_name = SC.class_name
AND Q.start_on <= now() 
AND Q.end_on >= now()
AND SC.user_student = 'student_2' //this is where I changed the student_1 to student_2
LEFT JOIN score S
ON Q.id = S.quiz_id
WHERE S.quiz_id IS NULL

Thank you for your answers 谢谢您的回答

It seems that the problem is that you don't check which student has taken the quiz when joining to the SCORE table. 似乎问题是您在加入SCORE表时没有检查哪个学生参加了测验。 Thus a quiz is excluded from the result if it is completed by any student. 因此,如果有任何学生完成测验,则测验将从结果中排除。

Try changing the query like this: 尝试像这样更改查询:

SELECT Q.id, Q.title, Q.start, Q.end, S.user_student, S.status
FROM quiz Q
INNER JOIN studentclass SC
ON Q.class_name = SC.class_name
AND Q.start_on <= now() 
AND Q.end_on >= now()
LEFT JOIN score S
ON Q.id = S.quiz_id
AND SC.user_student = S.user_student   -- Do not consider other students 
WHERE S.quiz_id IS NULL
AND SC.user_student = 'student_2'      -- moved from join clause for clarity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM