sqlserver 【数据库和SQL学习笔记】6.SELECT查询4:嵌套查询、对查询结果进行操作 - where C# = &#39

📅 2026-12-01 👤 拼多多助力营销技巧 📂 pdd助力 👁️ 139 次阅读

数据库系统软件:SQL Server 2019 Express 操作系统:Windows 10

文章目录

嵌套查询IN 与 NOT INEXISTS 与 NOT EXISTS比较运算符SOMEALL多层嵌套

对查询结果进行操作使用INTO语句将查询结果存入新表查询结果的并、交、差

本节接着学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,sqlserver

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧