SQL NOT EXISTS

 

 


看了一篇文章,虽然知识点很简单,但是还是帮我理解了一些以前没想到的东西


一共三个表student,class,score


create table student(sno varchar(50) not null,name varchar(50) not null) create table class(cno varchar(50) not null,name varchar(50) not null) create table score(sno varchar(50) not null,cno varchar(50) not null,score decimal(18,2) not null)
查询出选修了所有课程的学生:
select * from student s where not exists(select 1 from class c   where not exists(select 1 from score     where sno=s.sno and cno=c.cno));

梳理:1.select 1 from score where sno=s.sno and cno=c.cno
      
    通过学生编号和课程编号,查询出学生的选课记录
    前面加上not exists,就是没有选课的记录

    2.select 1 from class c
      where not exists(select 1 from score
        where sno=s.sno and cno=c.cno)
      学生没有选的课程

    3.select * from student s where not exists...(没有没选的课程)
      选了全部课程的学生的信息
原文http://blog.csdn.net/leftfist/article/details/44206081

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。