sqlserver exists,not exists的用法

php中文网
发布: 2016-06-07 17:59:33
原创
1854人浏览过

exists,not exists的使用方法示例,需要的朋友可以参考下。

学生表:create table student<BR>(<BR> id number(8) primary key,<BR> name varchar2(10),deptment number(8)<BR>)
登录后复制
选课表:create table select_course<BR>(<BR>  ID         NUMBER(8) primary key,<BR>  STUDENT_ID NUMBER(8) foreign key (COURSE_ID) references course(ID),<BR>  COURSE_ID  NUMBER(8) foreign key (STUDENT_ID) references student(ID)<BR>)
登录后复制
课程表:create table COURSE<BR>(<BR>  ID     NUMBER(8) not null,<BR>  C_NAME VARCHAR2(20),<BR>  C_NO   VARCHAR2(10)<BR>)
登录后复制
student表的数据:<BR>        ID NAME            DEPTMENT_ID<BR>---------- --------------- -----------<BR>         1 echo                   1000<BR>         2 spring                 2000<BR>         3 smith                  1000<BR>         4 liter                  2000
登录后复制
course表的数据:<BR>        ID C_NAME               C_NO<BR>---------- -------------------- --------<BR>         1 数据库               data1<BR>         2 数学                 month1<BR>         3 英语                 english1
登录后复制
select_course表的数据:<BR>        ID STUDENT_ID  COURSE_ID<BR>---------- ---------- ----------<BR>         1          1          1<BR>         2          1          2<BR>         3          1          3<BR>         4          2          1<BR>         5          2          2<BR>         6          3          2
登录后复制
1.查询选修了所有课程的学生id、name:(即这一个学生没有一门课程他没有选的。)
登录后复制
分析:如果有一门课没有选,则此时(1)select * from select_course sc where sc.student_id=ts.id 
登录后复制
and sc.course_id=c.id存在null,
登录后复制
这说明(2)select * from course c 的查询结果中确实有记录不存在(1查询中),查询结果返回没有选的课程,
登录后复制
此时select * from t_student ts 后的not exists 判断结果为false,不执行查询。
登录后复制
SQL> select * from t_student ts where <BR>	 (select * from course c where <BR>  		(select * from select_course sc where sc.student_id=ts.id and sc.course_id=c.id));        
登录后复制
        ID NAME            DEPTMENT_ID<BR>---------- --------------- -----------<BR>         1 echo                   1000
登录后复制
2.查询没有选择所有课程的学生,即没有全选的学生。(存在这样的一个学生,他至少有一门课没有选),
登录后复制
分析:只要有一个门没有选,即select * from select_course sc where student_id=t_student.id and course_id<BR>=course.id 有一条为空,即not exists null 为true,此时select * from course有查询结果(id为子查询中的course.id ),
登录后复制
因此select id,name from t_student 将执行查询(id为子查询中t_student.id )。
登录后复制
SQL> select id,name from t_student where 
登录后复制
登录后复制
	(select * from course where 
登录后复制
登录后复制
		(select * from select_course sc where student_id=t_student.id and course_id=course.id));
登录后复制
登录后复制
登录后复制
        ID NAME<BR>---------- ---------------<BR>         2 spring<BR>         3 smith<BR>         4 liter
登录后复制
3.查询一门课也没有选的学生。(不存这样的一个学生,他至少选修一门课程),
登录后复制
分析:如果他选修了一门select * from course结果集不为空,not exists 判断结果为false;
登录后复制
select id,name from t_student 不执行查询。
登录后复制
SQL> select id,name from t_student where 
登录后复制
登录后复制
	(select * from course where 
登录后复制
登录后复制
		(select * from select_course sc where student_id=t_student.id and course_id=course.id));
登录后复制
登录后复制
登录后复制
        ID NAME<BR>---------- ---------------<BR>         4 liter
登录后复制
4.查询至少选修了一门课程的学生。
SQL> select id,name from t_student where
登录后复制
	(select * from course where  
登录后复制
		(select * from select_course sc where student_id=t_student.id and course_id=course.id));
登录后复制
登录后复制
登录后复制
        ID NAME<BR>---------- ---------------<BR>         1 echo<BR>         2 spring<BR>         3 smith
登录后复制
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号