思路:1. 查询出每个系有多少课程 2. 查出学生选了对应系的课程数量 3,比对前面的数量,如相等,表示选了所有的课程
select distinnct b. student_name from
(select dept, count(course_no) as c_qty from course c group by dept) a,
( select s.name as student_name, s.dept, count(course_no) as c_qty from student s, c_s cs, course c where s.id=cs.student_id and s.dept=c.dept group by cs.student_name, s.dept ) b
where a.c_qty=b.c_qty and a.dept=b.dept
按上面的SQL试一下,应该是可行的。SQL还有优化空间的...
select student.name from student,course,c_s where student.dept=course.dept and course.course_no=c_s.course_no
select distinct s.name from student s where s.id in
(select cs.student_id from course c left join c_s cs on(c.course_no=cs.course_no)
where c.dept=s.dept)