当前位置:网站首页>【数据库和SQL学习笔记】6.SELECT查询4:嵌套查询、对查询结果进行操作
【数据库和SQL学习笔记】6.SELECT查询4:嵌套查询、对查询结果进行操作
2022-08-05 05:15:00 【takedachia】
数据库系统软件:SQL Server 2019 Express
操作系统:Windows 10
本节接着学select查询。
用到的数据库备份: teaching.bak
表结构回顾:
t_student (S#, Sname, Sex, Age, Major)
t_teacher (T#, Tname, Age, Title)
t_course (C#, Cname, T#)
t_student_course (S#, C#, Score)
嵌套查询
在一个select语句的from、where、having子句中,均可以嵌套另一组select语句,称为嵌套查询。
select查询的结果本质上也是一张表,所以要使用select查询结果,需要使用关键词。
常用关键词:
- IN(NOT IN)
- EXISTS(NOT EXISTS)
- 比较运算符
- 比较运算符+SOME(ALL)
下面直接用例子讲解。
IN 与 NOT IN
例1:查询选修了c001课程的学生的学号与姓名。
①多表查询解决方案:select t_student.S#, Sname from t_student, t_student_course where t_student.S# = t_student_course.S# and C#='c001'②嵌套查询解决方案:
思路:学生的学号和姓名在表 t_student,课程号信息在表 t_student_course,两者用外键 S# 联系。
我们分两步,先把选修了c001课程学生的学号找出来(得到集合);
再在这个学号范围内select出学生的学号和姓名(使用 IN + 集合)。
于是:select S#, Sname from t_student where S# in (select S# from t_student_course where C# = 'c001')
在本方法中,嵌套的查询(即子查询)与外层的查询是相互独立的,称为不相关子查询。
不相关子查询只需要执行一次。
``
③嵌套查询解决方案2:select S#, Sname from t_student where 'c001' in (select C# from t_student_course where S# = t_student.S#)在这里,查询的过程中,每查询出一个条目,都会就“c001”是否在另一个集合中进行判断。
而这另一个集合,每次都要在另一张表(t_student_course)中就S#为等值条件进行查询得出。
所以,在该方法中,外层查询的每一条记录都需要代入内层的查询中作为查询条件。外层有多少条记录,内层就要查询多少次,内外层的查询是有相关性的,称为相关子查询。
例2:查询未选修c001课程的学生的学号与姓名。
加NOT
①不相关子查询:select S#, Sname from t_student where S# not in (select S# from t_student_course where C# = 'c001')②相关子查询:
select S#, Sname from t_student where 'c001' not in (select C# from t_student_course where S# = t_student.S#)
EXISTS 与 NOT EXISTS
EXISTS 与 NOT EXISTS 意为是否存在这样的查询结果。
我们可以推断,在判断是否存在时,外层查询的每一条记录都需要代入内层的查询中作为查询条件,存在就输出到结果,因此也是一种相关子查询。
还是用同样的例子讲解:
例:查询选修了c001课程的学生的学号与姓名。
select S#, Sname from t_student where exists (select * from t_student_course where t_student_course.S# = t_student.S# and C# = 'c001')
比较运算符
使用比较运算符时,子查询的返回值一定要单个,不能是一个集合。
例:查询年龄比李华大的学生的信息。
select * from t_student where Age>(select Age from t_student where Sname='李华')
SOME
使用“some+集合”代表一个集合对象,用等号“=”连接。
和之前的 IN+集合 等效。
还是用之前的例子讲解:
例:查询选修了c001课程的学生的学号与姓名。
select * from t_student where S# = some(select S# from t_student_course where C#='c001')
ALL
用“all+集合”代表一个集合整体,用不等号“!=”连接。
和之前的 NOT IN+集合 等效。
例:查询未选修c001课程的学生的学号与姓名。
select * from t_student where S# != all(select S# from t_student_course where C#='c001')
多层嵌套
一个三层查询的例子:
例:查询考试平均分低于总平均分的学生的学号和姓名。
思路:最外层查询要查 t_student 这张表,我们需要取到符合条件的学生的学号S#集合。
符合条件的学号S#在 t_student_course 这张表中找。我们通过group by求平均,再用having限制上条件就能得到子查询。select * from t_student where S# in (select S# from t_student_course group by S# having avg(score) < (select avg(score) from t_student_course))
对查询结果进行操作
对查询结果进行操作包括:
- 储存查询结果
- 查询结果的并、交、差
使用INTO语句将查询结果存入新表
在select语句后接上into语句,可将查询结果存储在一张新表上。
语法:
SELECT ...
INTO 新表名
FROM ...
WHERE ...
例:查询学生选课信息,包括学号、姓名、课程名和成绩,将查询结果存放于表SCORE_LIST中。
select t_student.S#, Sname, t_course.Cname, Score into SCORE_LIST from t_student, t_student_course, t_course where t_student.S# = t_student_course.S# and t_student_course.C# = t_course.C#
完成后刷新一下数据库就能看到这张新表了。
查询结果的并、交、差
当多个查询结果集的列数、列顺序以及数据类型相同时,可以对它们进行并、交、差操作。
并、交、差语法:
SELECT语句1
UNION / INTERSECT / EXCEPT
SELECT语句2
其本质是集合的基本运算。
例:查询c001和c002两门课程均未考到90分的学生的学号。
select S# from t_student_course where C#='c001' and Score<90 intersect select S# from t_student_course where C#='c002' and Score<90
边栏推荐
- ES6 新特性:Class 的继承
- Convert the paper official seal in the form of a photo into an electronic official seal (no need to download ps)
- redis cache clearing strategy
- 基于Flink CDC实现实时数据采集(一)-接口设计
- 解决:Unknown column ‘id‘ in ‘where clause‘ 问题
- 02.01-----参数的引用的作用“ & ”
- spingboot 容器项目完成CICD部署
- 物理层的接口有哪几个方面的特性?各包含些什么内容?
- flink部署操作-flink standalone集群安装部署
- Lecture 5 Using pytorch to implement linear regression
猜你喜欢

【过一下10】sklearn使用记录

Flink 状态与容错 ( state 和 Fault Tolerance)

解决:Unknown column ‘id‘ in ‘where clause‘ 问题

The difference between the operators and logical operators

BroadCast Receiver(广播)详解
![[Go through 10] sklearn usage record](/img/70/60783c7d16000c6e9d753d8db9a330.png)
[Go through 10] sklearn usage record

数据库实验五 备份与恢复

第四讲 反向传播随笔
![[Over 17] Pytorch rewrites keras](/img/a2/7f0c7eebd119373bf20c44de9f7947.png)
[Over 17] Pytorch rewrites keras

BFC详解(Block Formmating Context)
随机推荐
【MySQL】数据库多表链接的查询方式
软件设计 实验四 桥接模式实验
数据库期末考试,选择、判断、填空题汇总
MySql之索引
Day1:用原生JS把你的设备变成一台架子鼓!
The software design experiment four bridge model experiment
Service
学习总结week3_3迭代器_模块
ES6 生成器
flink yarn-session的两种使用方式
关于基于若依框架的路由跳转
[Go through 4] 09-10_Classic network analysis
flink项目开发-配置jar依赖,连接器,类库
【论文精读】R-CNN 之预测框回归(Bounding box regression)问题详述
ES6基础语法
【过一下4】09-10_经典网络解析
有用番茄来监督自己的同道中人吗?加一下我的自习室,一起加油
vscode+pytorch use experience record (personal record + irregular update)
day6-列表作业
MySQL




