mySQL - 加入多个表

时间:2016-08-11 18:44:18

标签: php left-join inner-join

我有如下表格。我在我的Android测验应用程序中使用它。我想找那些不能参加测验的学生。

表格测验

+-------------------------------------------------------------------+
| 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    |
+-------------------------------------------------------------------+

表学生类

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

我的初步查询是:

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'

考虑今天的日期是2016-08-02

我可以获取测验表的第一行和第二行

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

现在我还想要与之前的查询一起获取分数表。学生'student_1'完成了测验。

表评分

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

我的新查询

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

结果是

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

结果很好但是当我在第二次查询中将student_1更改为student_2时,我仍然得到相同的结果。 student_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

感谢您的回答

1 个答案:

答案 0 :(得分:0)

似乎问题是你在加入SCORE表时没有检查哪个学生参加了测验。因此,如果测验由任何学生完成,则测验将从结果中排除。

尝试按以下方式更改查询:

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