第三章:数据查询语言DQL-子查询exists和in的比较
发布日期:2021-05-07 07:28:44 浏览次数:25 分类:精选文章

本文共 719 字,大约阅读时间需要 2 分钟。

直接学习:https://edu.csdn.net/course/play/27328/370702

子查询exists和in的比较
#1、子查询 in :如果运算符in 后面的值来源于某个查询结果,并非是指定的几个值,这时就需要用用到子查询,子查询又称为内部查询或嵌套查询,即在SQL查询的where子句中嵌入查询结果
#2、代码示例:

查询学生表中所有选修了课程的学生信息

select A.*
from student A
where A.stu_no in (select B.stu_no from score B);

查询学生表中选修了离散数学课程的学生信息

select A.*
from student A
where A.stu_no in (select B.stu_no from score B where B.course = ‘离散数学’);

#3、子查询exists:exists是子查询中用于测试内部查询是否返回任何行的布尔运算符。将朱查询的数据放入子查询中做条件验证,根据验证结果(True或False)来决定主查询结果数据是否保留。

#4、代码示例如下:
查询学生表中所有选修了课程的学生信息
select A.*
from student A
where exists (select B.* from score B where A.stu_no = B.stu_no);

查询学生表中没选修课程的学生信息

select A.*
from student A
where not exists (select B.* from score B where A.stu_no = B.stu_no);

上一篇:第四章:数据控制语言DCL-用户管理
下一篇:第三章:数据查询语言DQL-自连接查询的场景及使用

发表评论

最新留言

表示我来过!
[***.240.166.169]2025年04月19日 02时47分00秒