首页 > 数据库技术 > 详细

SQL NOT EXISTS

时间:2015-03-24 14:23:47      阅读:162      评论:0      收藏:0      [点我收藏+]

 

 


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


一共三个表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

SQL NOT EXISTS

原文:http://www.cnblogs.com/anglee/p/4362537.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!