0

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

有几张桌子...

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 个月。换句话说,选择所有在过去六个月内没有参加过特定教师课程的用户。

这让我有点困惑,所以任何帮助都将不胜感激。

4

2 回答 2

0
select username 
from user 
where userid not in 
(select userid 
from attendance a, course c 
where a.courseid = c.courseid and teacherid = {Value})
于 2013-09-06T11:26:02.823 回答
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 曾经(并且可能仍然)不擅长优化子查询。

于 2013-09-06T13:18:13.967 回答