【MySQL】_自连接与子查询、

目录

[1. 自连接](#1. 自连接)

[2. 子查询(嵌套查询)](#2. 子查询(嵌套查询))

[2.1 子查询分类](#2.1 子查询分类)

[2.2 单行子查询示例1:查询不想毕业同学的同班同学](#2.2 单行子查询示例1:查询不想毕业同学的同班同学)

[2.3 多行子查询示例2:查询语文或英语课程的信息成绩](#2.3 多行子查询示例2:查询语文或英语课程的信息成绩)

[3. 合并查询](#3. 合并查询)

[3.1 示例1:查询id=3或者名字为英文的课程](#3.1 示例1:查询id=3或者名字为英文的课程)


本专栏关于联合查询已建立相应库与表,原文链接如下:

【MySQL】_联合查询基础表-CSDN博客

内连接原文如下:

【MySQL】_内连接-CSDN博客

外连接原文如下:

【MySQL】_外连接-CSDN博客

基于以上内容,本篇介绍自连接与子查询、合并查询;

1. 自连接

自连接是表自身与自身做笛卡尔积,在SQL中进行条件查询,都是指定某一列或多个列之间进行关系运算,无法进行行与行之间的运算,在某些情况下需要对行与行之间进行关系运算,就要使用到自连接。自连接的本质是将行转为列

示例:显示所有"课程id为3"比"课程id为1"成绩高的成绩信息:

(成绩信息在score表中)

(1)对score进行自连接(别名求笛卡尔积)并删除无效信息:

sql 复制代码
mysql> select* from score as s1, score as s2 where s1.student_id = s2.student_id;

(2)选出第一列id=1的课程与第二列id=3的课程:

sql 复制代码
mysql> select* from score as s1, score as s2
    -> where s1.student_id = s2.student_id
    -> and s1.course_id = 1
    -> and s2.course_id = 3;

(该结果表示有三个同学同时选修了这两门课程)

(3)增加左列成绩小于右列成绩条件,SQL指令与查询结果为:

sql 复制代码
mysql> select* from score as s1,score as s2
    -> where s1.student_id = s2.student_id
    -> and s1.course_id = 1
    -> and s2.course_id = 3
    -> and s1.score < s2.score;
+-------+------------+-----------+-------+------------+-----------+
| score | student_id | course_id | score | student_id | course_id |
+-------+------------+-----------+-------+------------+-----------+
|  70.5 |          1 |         1 |  98.5 |          1 |         3 |
|  33.0 |          3 |         1 |  68.0 |          3 |         3 |
+-------+------------+-----------+-------+------------+-----------+
2 rows in set (0.00 sec)

注:(1)不能直接进行自连接:

sql 复制代码
mysql> select* from score,score;
ERROR 1066 (42000): Not unique table/alias: 'score'

需要为表指定两个别名,即:

sql 复制代码
mysql> select* from score as s1, score as s2;

2. 子查询(嵌套查询)

子查询是指嵌入其他SQL语句中的select语句,即将多个查询语句合并为一个语句;

2.1 子查询分类

(1)单行子查询:查询结果只有一条记录;

(2)多行子查询:查询结果为多条记录;

2.2 单行子查询示例1:查询不想毕业同学的同班同学

(1)分步查询SQL指令及查询结果为:

sql 复制代码
mysql> select classes_id from student where name="不想毕业";
+------------+
| classes_id |
+------------+
|          1 |
+------------+
1 row in set (0.00 sec)

mysql> select name from student where classes_id =1;
+------------+
| name       |
+------------+
| 黑旋风李逵 |
| 菩提老祖   |
| 白素贞     |
| 许仙       |
| 不想毕业   |
+------------+
5 rows in set (0.00 sec)

(2)子查询SQL指令及查询结果为:

sql 复制代码
mysql> select name from student where classes_id = (select classes_id from student where name="不想毕业");
+------------+
| name       |
+------------+
| 黑旋风李逵 |
| 菩提老祖   |
| 白素贞     |
| 许仙       |
| 不想毕业   |
+------------+
5 rows in set (0.00 sec)

即将条件查询的某一个值替换为一个select查询语句;

2.3 多行子查询示例2:查询语文或英语课程的信息成绩

先查询出两个课程的课程id,再根据course_id在score表中查询;

(1)分步查询SQL指令及查询结果为:

sql 复制代码
mysql> select id from course where name="语文" or name="英文";
+----+
| id |
+----+
|  4 |
|  6 |
+----+
2 rows in set (0.00 sec)

mysql> select* from score where course_id in(4,6);
+-------+------------+-----------+
| score | student_id | course_id |
+-------+------------+-----------+
|  98.0 |          1 |         6 |
|  72.0 |          4 |         6 |
|  43.0 |          6 |         4 |
|  79.0 |          6 |         6 |
|  92.0 |          7 |         6 |
+-------+------------+-----------+
5 rows in set (0.00 sec)

(2)子查询SQL指令及查询结果为:

sql 复制代码
mysql> select* from score where course_id in(select id from course where name="语文" or name="英文");
+-------+------------+-----------+
| score | student_id | course_id |
+-------+------------+-----------+
|  98.0 |          1 |         6 |
|  72.0 |          4 |         6 |
|  43.0 |          6 |         4 |
|  79.0 |          6 |         6 |
|  92.0 |          7 |         6 |
+-------+------------+-----------+
5 rows in set (0.00 sec)

3. 合并查询

合并查询就是将两个查询语句的结果合并到一起;

3.1 示例1:查询id=3或者名字为英文的课程

(1)使用逻辑或实现查询:

sql 复制代码
mysql> select* from course where id<3 or name="英文";
+----+--------------+
| id | name         |
+----+--------------+
|  1 | Java         |
|  2 | 中国传统文化 |
|  6 | 英文         |
+----+--------------+
3 rows in set (0.00 sec)

(2)使用union关键字进行合并查询:

sql 复制代码
mysql> select* from course where id<3 union select* from course where name="英文";
+----+--------------+
| id | name         |
+----+--------------+
|  1 | Java         |
|  2 | 中国传统文化 |
|  6 | 英文         |
+----+--------------+
3 rows in set (0.00 sec)

注:(1)union与逻辑或的区别:

逻辑或只能对一张表的查询结果进行合并,但union可以对多张表的查询结果进行合并(要求多个结果的列须对应)

(2)union与union all的区别:

使用union关键字对多个查询结果进行合并时会自动去重,但unionall不会去重

相关推荐
IvorySQL27 分钟前
PG 日报|新增 VACUUM 全维度统计信息补丁迭代
数据库·人工智能·postgresql·ivorysql
白露与泡影3 小时前
Redis 缓存实战:雪崩、穿透、击穿一次讲透
数据库·redis·缓存
赵渝强老师4 小时前
【赵渝强老师】使用Oracle可传输的表空间迁移数据
数据库·oracle
IpdataCloud5 小时前
跨区服玩家延迟高怎么办?用IP离线库自动分配到最近服务器
数据库·tcp/ip·github·php·ip
wuqingshun3141596 小时前
MYSQL默认的事务隔离级别是什么?为什么选择这个级别?
数据库·mysql
空中湖6 小时前
电池简史(五):极限之梦与终章——锂硫、锂空气、量子电池,以及你该知道的电池投资与生活指南
数据库·程序人生
蓝胖子酱8 小时前
在 Electron 中用 electron-store 实现加密存储与自定义格式文件
前端·javascript·数据库
snow@li8 小时前
Redis:数据库语法速查表 / 全景梳理
数据库·redis·缓存
Devlive 开源社区8 小时前
Nebula 1.6.0 发布:跨区域桶自动路由、秒传、文本/PDF 预览一次到位
数据库·sql
这个DBA有点耶9 小时前
导个数据中文全变问号?字符集这事儿真没那么简单
数据库·mysql·代码规范