查詢選修了全部課程的學生姓名_如何解?-MS-SQLServer/應用實例
阿新 • • 發佈:2017-08-07
sqlserver sna sts bsp 課程 這一 學生 .cn 全部
------其他回答(10分)---------
查詢選修了全部課程的學生姓名
:student 是學生表 course 是選課表 sc 是課程表
select sname
from student
where not exists
(select *
from course
where not exists
(select *
from sc
where sno =student.sno
and cno=course.cno)
如何解釋呢?我只是約約的明白,但就是給別人解釋不明白。希望高手給解釋一下。
------其他回答(10分)---------
select *
from course
where not exists
(select *
from sc
where sno =student.sno
and cno=course.cno)
這一段計算得出 某學生存在沒有選的課程
如果外層再加上 一個不存在 沒有選的課程。那就是全選了
- SQL code
- -- 從裏往外分析 select * from student -- (3) where not exists ( select * from course -- (2) where not exists ( select * from sc where sno=student.sno -- (1) and cno=course.cno ) ); -- (1) 查找某一個學生所選的所有課程 select * from sc where sno=student.sno -- (2) 查找這個學生沒有選修的課程 select * from course c where not exists ( select * from sc where sno=student.sno and cno=course.cno) -- (3) 排除這個沒有選修所有課程的學生
負負得正,不存在任何一科沒有選修的,也就是每科都選修了
查詢選修了全部課程的學生姓名_如何解?-MS-SQLServer/應用實例