PHP mySQL - 从数据库中的3个表中获取数据

时间:2016-08-11 08:26:09

标签: php mysql

我的列有3个表格,我想取得那些没有参加过测验的学生和那些已经参加过测验的学生(单独的剧本)

Table quiz
Column id, title, created_at, start_on, end_on, class_name, user_faculty

Table studentclass
Column id, class_name, user_faculty, user_student

Table score
Column id, grade, user_student, status, quiz_id

quiz.class_name = studentclass.class_name,quiz.user_faculty = sctudentclass.user_faculty,studentclass.user_student = score.user_student and quiz.id = score.quiz_id

score.status ='F'//这定义了user_student已经参加了测验

我的查询是:

SELECT quiz.id, quiz.title, quiz.start_on, quiz.end_on, quiz.class_name,
quiz.user_faculty, studentclass.user_student, score.status
FROM quiz
INNER JOIN studentclass
ON quiz.class_name = studentclass.class_name
AND start_on >= now()
AND end_on <= now()
LEFT JOIN score
ON quiz.id = score.quiz_id
WHERE score.status IS NULL

如果user_student已经参加了测验,那么他将无法再看到测验,因为他已经在得分表中有一行包含user_student,quiz_id和status。但是其他未参加测验的user_student可以看到测验。

我希望你得到我想弄清楚的东西。

1 个答案:

答案 0 :(得分:0)

以下查询将列出所有参加过测验的学生。如果它符合您的目的,请告诉我。我会根据需要更新它。

如果需要其他表中的数据,可以相应地添加联接。

select * from studentclass Where id NOT IN (
     select user_student from score where quiz_id = YOUR ID //if you wnat to filter it for a specific quiz.
)