Anglee的自留地

一个新手的自白

导航

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

posted on 2015-03-24 13:40  Anglee的自留地  阅读(158)  评论(0编辑  收藏  举报