sqlserver exists,not exists的用法

所属分类: 数据库 / MsSql 阅读数: 734
收藏 0 赞 0 分享
学生表:create table student
(
 id number(8) primary key,
 name varchar2(10),deptment number(8)
)
选课表:create table select_course
(
  ID         NUMBER(8) primary key,
  STUDENT_ID NUMBER(8) foreign key (COURSE_ID) references course(ID),
  COURSE_ID  NUMBER(8) foreign key (STUDENT_ID) references student(ID)
)
课程表:create table COURSE
(
  ID     NUMBER(8) not null,
  C_NAME VARCHAR2(20),
  C_NO   VARCHAR2(10)
)
student表的数据:
        ID NAME            DEPTMENT_ID
---------- --------------- -----------
         1 echo                   1000
         2 spring                 2000
         3 smith                  1000
         4 liter                  2000
course表的数据:
        ID C_NAME               C_NO
---------- -------------------- --------
         1 数据库               data1
         2 数学                 month1
         3 英语                 english1
select_course表的数据:
        ID STUDENT_ID  COURSE_ID
---------- ---------- ----------
         1          1          1
         2          1          2
         3          1          3
         4          2          1
         5          2          2
         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 not exists
 (select * from course c where not exists
   (select * from select_course sc where sc.student_id=ts.id and sc.course_id=c.id));       
        ID NAME            DEPTMENT_ID
---------- --------------- -----------
         1 echo                   1000
2.查询没有选择所有课程的学生,即没有全选的学生。(存在这样的一个学生,他至少有一门课没有选),
分析:只要有一个门没有选,即select * from select_course sc where student_id=t_student.id and course_id
=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 exists
	(select * from course where not exists
		(select * from select_course sc where student_id=t_student.id and course_id=course.id));
        ID NAME
---------- ---------------
         2 spring
         3 smith
         4 liter
3.查询一门课也没有选的学生。(不存这样的一个学生,他至少选修一门课程),
分析:如果他选修了一门select * from course结果集不为空,not exists 判断结果为false;
select id,name from t_student 不执行查询。
SQL> select id,name from t_student where not exists
	(select * from course where exists
		(select * from select_course sc where student_id=t_student.id and course_id=course.id));
        ID NAME
---------- ---------------
         4 liter
4.查询至少选修了一门课程的学生。
SQL> select id,name from t_student where exists
	(select * from course where  exists
		(select * from select_course sc where student_id=t_student.id and course_id=course.id));
        ID NAME
---------- ---------------
         1 echo
         2 spring
         3 smith
更多精彩内容其他人还在看

SQL Server正确删除Windows认证用户的方法

这篇文章主要给大家介绍了关于SQL Server正确删除Windows认证用户的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用SQL Server具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

SQL Server查看login所授予的具体权限问题

在SQL Server数据库中如何查看一个登录名(login)的具体权限呢,下面脚本之家小编给大家带来了SQL Server查看login所授予的具体权限问题,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

Mysql8.0.17安装教程【推荐】

本文通过图文并茂的形式给大家介绍了Mysql8.0.17安装,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C# ling to sql 取多条记录最大时间

这篇文章主要介绍了C# ling to sql 取多条记录最大时间,文中通过实例代码给大家介绍了sql 查询相同记录下日期最大的 一条,代码简单易懂,需要的朋友可以参考下
收藏 0 赞 0 分享

sql server编写archive通用模板脚本实现自动分批删除数据

这篇文章主要介绍了sql server编写archive通用模板脚本实现自动分批删除数据,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL Server怎么找出一个表包含的页信息(Page)

这篇文章主要给大家介绍了关于SQL Server是如何找出一个表包含的页信息(Page)的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用SQL Server具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

SQLyog连接MySQL8.0报2058错误的完美解决方法

这篇文章主要介绍了SQLyog连接MySQL8.0报2058错误的完美解决方法,本文图文并茂给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL SERVER日志进行收缩的图文教程

这篇文章主要给大家介绍了关于SQL SERVER日志进行收缩的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用SQL SERVER具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

SqlServer中批量update语句

我现在想把S_USER表中的ACCOUNT批量修改成S_PERSON的ACCOUNT,那么就可以参考下面的语句,要找到对应的字段
收藏 0 赞 0 分享

SqlServer AS的用法

本篇文章简要分析,在SQL SERVER中,AS的基本用法,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多