第三章:数据查询语言DQL-子查询exists和in的比较

直接学习: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);

发布了107 篇原创文章 · 获赞 6 · 访问量 968

猜你喜欢

转载自blog.csdn.net/weixin_43597208/article/details/105464756