当前位置: 代码网 > it编程>数据库>MsSqlserver > SqlServer中Exists的使用小结

SqlServer中Exists的使用小结

2024年05月15日 MsSqlserver 我要评论
1、简介不相关子查询:子查询的查询条件不依赖于父查询的称为不相关子查询相关子查询:子查询的查询条件依赖于外层父查询的某个属性值的称为相关子查询。带exists的子查询就是相关子查询exists表示存在

1、简介

  • 不相关子查询:子查询的查询条件不依赖于父查询的称为不相关子查询
  • 相关子查询:子查询的查询条件依赖于外层父查询的某个属性值的称为相关子查询。带exists的子查询就是相关子查询
  • exists表示存在量词:带有exists的子查询不返回任何记录的数据,只返回逻辑值“true”或“false”

2、表结构

选课表:学号studentno、课程号courseno

学生表:学号studentno、姓名studentname

课程表:课程号courseno、课程名coursename

3、查询所有选修了“c1”课程的学生名

in语句查询:

select studentname from 学生表
where studentno in (select studentno from 选课表 where courseno=‘c1')

exists查询:

select studentname from 学生表
where exists (select 1 from 选课表 where 选课表.studentno=学生表.studentno and 选课表.courseno='c1')

相关子查询执行过程:先在外层查询中取“学生表”的第一行记录,利用该记录的相关属性值(在exists子查询的where子句中用到的列)处理内层查询,若外层的where子句返回“true”,则本条记录放入结果表中。然后再取下一行记录,重复上述过程直到外层表遍历完毕。

exists语句不关心子查询返回的具体内容,因此用“exists(select 1 from)”来判断子查询是否返回记录。

  • exists(select):若子查询的结果集非空时,exists()表达式返回true;子查询的结果集为空时,exists()表达式返回false。
  • not exists(select):若子查询的结果集非空时,not exists()表达式返回false;子查询的结果集为空时,not exists()表达式返回true。

4、查询没所有选修“c1”课程的学生名

select studentname from 学生表
where not exists (select 1 from 选课表 where 学生表.studentno=选课表.studentno and courseno=‘c1')

5、查询选修了所有课程的学生名

--外层查询、外层not exists
select studentname from 学生表 where not exists 
(    
    --内层查询、内层not exists
    select 1 from 课程表 where not exists
    (
        select 1 from 选课表 where 学生表.studentno=选课表.studentno and 课程表.courseno=选课表.courseno
    )
)

a、选一行学生信息s1、选一行课程信息c1
内层的not exists()值为true,说明选课表中找不到“s1.studentno + c1.courseno”这一记录,说明学生s1没有选课程c1,此时内层查询的返回结果集会加上c1,当内层查询的返回结果集不为空时,外层not exists()值为false,则外层where子句值为false,则s1被排除。
当内层查询的返回结果集不为空时,说明s1至少有一门课程没选 。

b、选一行学生信息s1、选一行课程信息c2
内层的not exists()值为false,说明选课表中有“s1.studentno + c2.courseno”这一记录,说明学生s1选了课程c2,此时内层查询的返回结果集不会加上c2,当内层查询的返回结果集为空时,外层not exists()值为true,则外层where子句值为true,则s1被选中。
当内层查询的返回结果集为空时,说明s1已经选了所有课程。 

c、结果
外层查询最终返回的结果是选择了所有课程的学生。

6、查询选修了c1课程和c2课程的学生名

--外层查询、外层not exists
select studentname from 学生表 where not exists 
(    
    --内层查询、内层not exists
    select 1 from 课程表 where courseno in('c1','c2') and not exists
    (
        select 1 from 选课表 where 学生表.studentno=选课表.studentno and 课程表.courseno=选课表.courseno
    )
)

第五条查询的是选修了所有课程的学生,如果我们将所有课程限定为“c1、c2”,那查询结果就变为选修了c1、c2的学生,该结果保证学生至少选修了c1、c2,但是选没选其他课不清楚。

7、查询至少选修了s1所选的全部课程的学生名

--外层查询、外层not exists
select studentname from 学生表 where not exists 
(    
    --内层查询、内层not exists
    select 1 from 选课表x where 选课表x.studentno='s1' and not exists
    (
        select 1 from 选课表y where 学生表.studentno=选课表y.studentno and 选课表x.courseno=选课表y.courseno
    )
)

第五条查询的是选修了所有课程的学生,如果我们将所有课程限定为s1所选的全部课程,那查询结果就变为选修了s1所选的全部课程的学生,该结果保证学生至少选修了s1所选的全部课程,但是选没选其他课不清楚。

8、在from语句中使用子查询,对查询结果定义表名及列名

--定义表名可以用as也可以不用as
select studentname,avgscore,createdate from
(select studentname,createdate,avg(score) from studentscores group by studentname,createdate)as ta(studentname,avgscore,createdate)
where createdate>80

--定义表名可以用as也可以不用as
select studentname,avgscore,createdate from
(select studentname,createdate,avg(score) from studentscores group by studentname,createdate)ta(studentname,avgscore,createdate)
where createdate>80

到此这篇关于sqlserver中exists的使用小结的文章就介绍到这了,更多相关sqlserver exists内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网! 

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com