我现在有一个用户表user 课程表 course 用户课程表usercourse
现在我要查没有学习courids这些课程的用户 只要用户没有学习courseids这里边的任何一个课程就算没有学校这些课程
code="sql"
INTERSECT
( select userId from usercourse where usercourse.courseid = 2))
INTERSECT
(select userId from usercourse where usercourse.courseid = 3) [/code]
比如如下数据
userId courseId
1 1
2 4
3 2
4 3
1 2
1 3
3 1
3 3
这样的数据,用上面的语句查出来的是1和3,因为userId为1,3的学生修过课程好为(1,2,3)的这三门课。
至少把表结构贴出来吧
usercourse表中应该有字段userid和courseid的吧
那就这么查
[code="sql"]select * from user
where userid not in
(
select distinct usercourse.userid
from course,usercourse
where course.courseid = usercourse.courseid
)[/code]
[color=red]使用INTERSECT[/color]
+++++++++++++++++++++++++++++++++++++++=
假设courseids包括(1,2,3)这样Id的课程。
那么sql语句如下
[code="sql"]select * from User
where userId not in(
((select userId from usercourse where usercourse.courseid = 1)
INTERSECT
( select userId from usercourse where usercourse.courseid = 2))
INTERSECT
(select userId from usercourse where usercourse.courseid = 3)
)[/code]
不是一样的,[code="sql"]select * from User
where userId not in(1,2,3); [/code]