本人现遇到如下问题:
学生 student 和课程 course 是一个多对多关系
一个student 可以拥有多个 course ;同样 一个course 也可能拥有多个学生
有一中间表表示他们关系 tb_student_course 如下
student course
1 1
1 2
2 1
2 2
2 3学生1拥有: 1,2 俩课程 学生2拥有 1,2,3 课程
现在 我想从这张表中 查找只是拥有 1,2 俩课程的学生 (结果是学生一)
如何写sql? 哪位帅哥美女 能帮忙解答下 谢谢拉 :>
学生 student 和课程 course 是一个多对多关系
一个student 可以拥有多个 course ;同样 一个course 也可能拥有多个学生
有一中间表表示他们关系 tb_student_course 如下
student course
1 1
1 2
2 1
2 2
2 3学生1拥有: 1,2 俩课程 学生2拥有 1,2,3 课程
现在 我想从这张表中 查找只是拥有 1,2 俩课程的学生 (结果是学生一)
如何写sql? 哪位帅哥美女 能帮忙解答下 谢谢拉 :>
declare @t table(student int,course int)
insert @t select 1,1
union all select 2,2
union all select 2,3
union all select 1,2
union all select 2,1
union all select 3,2
union all select 3,5select student from @t group by student having count(1)=2 and student in (1,2)
---test
declare @t table(student int,course int)
insert @t select 1,1
union all select 2,2
union all select 2,3
union all select 1,2
union all select 2,1
union all select 3,2
union all select 3,5select student from @t group by student having count(1)=2 and student in (1,2)
-------------
student
1
where course in (1,2) and
(select COUNT(1) from @t where student=a.student)=2
where student in(select student from @t where course in (1,2))
group by student having count(1)=2 /*
student
-----------
1
*/