• 数据库-使用in的子查询,any,all修改的比较运算符,exists的子查询


    1.使用in的子查询

    1.1 概念

    • 过in引入的子查询结果是包含零个值或多个值得列表,子查询返回结果之后,外部查询将利用这些结果

    1.2 in列表

    查询在sc表中选修了课程的学生的信息
    select *from student   where sno in (select distinct sno from sc) 
    //子查询得到学生的学号,外部查询根据学号找到学生
    

    1.3 not in

    查询没有选修过任何课程的学生的信息
    select * from student   where  sno not in (select distinct sno from sc)
    //not in表示字段的值不在后面的子查询返回到结果中
    

    2.用any,all修改的比较运算符

    2.1 概念

    • 可以用all或者any修改引入子查询的比较运算符。
    • some是与any等效的ISO标准,以>比较运算符为例,>all表示大于每一个值,表示大于最大值。
    • 例如,>all(1,2,3)表示大于3,>any表示至少大于一个值,即大于最小值,因此>any(1,2,3)表示大于1

    2.2 实例

    在教师列表中,检索比任何一个女教师年龄都大的男教师的信息 
    select *from teacher where tsex='男' and tage>all(select tage from teacher where tsex='女')子查询得到每一位女教师的年龄,外层查询使用“>all”的语法,即比集合中最大值还大
    

    3.使用exists的子查询

    • 使用exists关键字引入子查询后,子查询的作用就相当于进行存在测试
    • 外部查询的where子句测试子查询返回的行是否存在
    • 子查询实际上不产生任何数据,它只返回TRUE或flase值

    3.1 exists

    查询选修了B004课程的学生的基本信息
    select *from student where exists (select *from sc where   sno=student.sno  and cno='B004')
    

    3.2 not exists

    查询没有选修X001课程的学生的基本信息
    select *from student where not exists (select *from sc where   sno=student.sno  and cno='X001')
    

    3.3 where exists

    查询与王国在同一个专业学习的所有学生的基本信息
    select sno,sname,smajor from student s1 where exists (select *from student s2 where s1.smajor=s2.smajor and s2.name='王国')
  • 相关阅读:
    CSS属性之定位
    CSS选择器区别
    HTML属性及其相关区别
    HTML标签区别
    HTML其他概念
    CSS3新特性
    HTML5新标签
    HTML5新特性
    params修饰符的用法
    C#中引用参数ref和输出参数out
  • 原文地址:https://www.cnblogs.com/dongxuelove/p/12972831.html
Copyright © 2020-2023  润新知