zoukankan      html  css  js  c++  java
  • SqlServer中Exists的使用

    SqlServer中Exists的使用

    2019-03-03 10:03:24 changuncle 阅读数 2515更多

    分类专栏: SQL基础

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

    本文链接:https://blog.csdn.net/xiaouncle/article/details/88084889

    1、简介

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

    2、表结构

    选课表:学号StudentNo、课程号CourseNo

    学生表:学号StudentNo、姓名StudentName

    课程表:课程号CourseNo、课程名CourseName

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

    In语句查询:

    
     
    1. select StudentName from 学生表

    2. where StudentNo in (select StudentNo from 选课表 where CourseNo=‘C1’)

    Exists查询:

    
     
    1. select StudentName from 学生表

    2. 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()表达式返回true;子查询的结果集为空时,not exists()表达式返回false。

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

    
     
    1. select StudentName from 学生表

    2. where not exists (select 1 from 选课表 where 学生表.StudentNo=选课表.StudentNo and CourseNo=‘C1’)

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

    
     
    1. --外层查询、外层not exists

    2. select StudentName from 学生表 where not exists 

    3. (

    4. --内层查询、内层not exists

    5. select 1 from 课程表 where not exists

    6. (

    7. select 1 from 选课表 where 学生表.StudentNo=选课表.StudentNo and 课程表.CourseNo=选课表.CourseNo

    8. )

    9. )

    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课程的学生名

    
     
    1. --外层查询、外层not exists

    2. select StudentName from 学生表 where not exists 

    3. (

    4. --内层查询、内层not exists

    5. select 1 from 课程表 where CourseNo in('C1','C2') and not exists

    6. (

    7. select 1 from 选课表 where 学生表.StudentNo=选课表.StudentNo and 课程表.CourseNo=选课表.CourseNo

    8. )

    9. )

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

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

    
     
    1. --外层查询、外层not exists

    2. select StudentName from 学生表 where not exists 

    3. (

    4. --内层查询、内层not exists

    5. select 1 from 选课表X where 选课表X.StudentNo='S1' and not exists

    6. (

    7. select 1 from 选课表Y where 学生表.StudentNo=选课表Y.StudentNo and 选课表X.CourseNo=选课表Y.CourseNo

    8. )

    9. )

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

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

    
     
    1. --定义表名可以用as也可以不用as

    2. select StudentName,avgScore,CreateDate from

    3. (select StudentName,CreateDate,AVG(Score) from StudentScores group by StudentName,CreateDate)as ta(StudentName,avgScore,CreateDate)

    4. where CreateDate>80

    5.  
    6. --定义表名可以用as也可以不用as

    7. select StudentName,avgScore,CreateDate from

    8. (select StudentName,CreateDate,AVG(Score) from StudentScores group by StudentName,CreateDate)ta(StudentName,avgScore,CreateDate)

    9. where CreateDate>80

    最后,我要感谢http://www.cnblogs.com/jiangyunfeng/p/9054305.html的无私奉献!

    有 0 个人打赏

  • 相关阅读:
    Leetcode Substring with Concatenation of All Words
    Leetcode Divide Two Integers
    Leetcode Edit Distance
    Leetcode Longest Palindromic Substring
    Leetcode Longest Substring Without Repeating Characters
    Leetcode 4Sum
    Leetcode 3Sum Closest
    Leetcode 3Sum
    Leetcode Candy
    Leetcode jump Game II
  • 原文地址:https://www.cnblogs.com/grj001/p/12223822.html
Copyright © 2011-2022 走看看