MySQL查询帮助 - 我很困惑

时间:2013-09-06 11:03:25

标签: mysql

任何人都想知道可以帮助我解决项目中出现的一些问题。该项目是一个人员及其参加培训课程的数据库。

有几张桌子......

Users (with UserID and Username)

Teachers (with TeacherID and TeacherName)

Courses (with CourseID, CourseName, CourseDate and TeacherID) - this one relates to which teacher took which course

Attendances (with UserID and CourseID) - this one relates which user attended which course

客户首先选择的是所有没有参加过特定教师课程的用户。换句话说,如果他们与指定教师一起上课,则不会显示。

其次,客户端需要与第一个查询相同的查询,但也将其限制为过去6个月。换句话说,选择在过去六个月内没有参加过特定教师课程的所有用户。

这个让我感到有些困惑,所以任何帮助都会受到赞赏。

2 个答案:

答案 0 :(得分:0)

select username 
from user 
where userid not in 
(select userid 
from attendance a, course c 
where a.courseid = c.courseid and teacherid = {Value})

答案 1 :(得分:0)

SELECT u.username
FROM user u
LEFT JOIN attendance a
  ON u.userid = a.userid
LEFT JOIN course c
  ON c.courseid = a.courseid
  AND teacherid = [teacher id value]
WHERE a.courseid IS NULL

可能比NOT IN子查询更快。 MySQL曾经(并且可能仍然)在优化子查询方面表现不佳。