【MySQL探索之旅】多表查询

在这里插入图片描述

📚博客主页:爱敲代码的小杨.

✨专栏:《Java SE语法》 | 《数据结构与算法》 | 《C生万物》 |《MySQL探索之旅》 |《Web世界探险家》

❤️感谢大家点赞👍🏻收藏⭐评论✍🏻,您的三连就是我持续更新的动力❤️

🙏小杨水平有限,欢迎各位大佬指点,相互学习进步!

1. 多表查询的概念

多表查询就是对多表查询我们需要的数据. 通过笛卡尔积进行查询

1.1 笛卡尔积

百度百科:

笛卡尔乘积是指在数学中,两个集合X和Y的笛卡尓积(Cartesian product),又称直积,表示为X×Y,第一个对象是X的成员而第二个对象是Y的所有可能有序对的其中一个成员。

image-20240416153106532

案例:

image-20240416153733369

笛卡尔积就是得到了一个更大的表. 列数就是原来两个表列数的之和. 行数就是原来两个表行数之乘.

【注意】:笛卡尔积是全排列的过程,在尝试穷举所有的可能性,自然就会产生一些不符合实际情况的数据

上述案例就有一部分为无效数据/无意义的数据

初始化数据:

create table student(id int, name varchar(20), classId int);
inset into student values (1,'张三',1);
insert into student values (2,'李四',2);
insert into student values (3,'赵五',3);
insert into student values (4,'赵六',4);

create table class(classId int, className varchar(20));
insert into class values (1,'计算机科学1班');
insert into class values (2,'软件工程1班');
insert into class values (3,'人工智能1班');
insert into class values (5,'信息安全1班');

2. 连接查询

2.1 内连接

语法:

-- 显示内连接
select 字段 from1 别名1 [inner] join2 别名2 on 连接条件 and 其他条件;
-- 隐式内连接
select 字段 from1 别名1,2 别名2 where 连接条件 and 其他条件;

案例1:

-- 查询张三的信息
select * from student, class where student.classId = class.classId and name = '张三';
-- 或者
select * from student inner join class on student.classId = class.classId and name = '张三';

运行结果:

image-20240416160146712

案例2:

-- 查询所有有班级的学生的信息
select * from student inner join class on student.classId = class.classId;
-- 或者
select * from student, class where student.classId = class.classId;

运行结果:

image-20240416160536306

2.2 外连接

外连接分为左外连接和右外连接。如果联合查询,左侧的表完全显示我们就说是左外连接;右侧的表完
全显示我们就说是右外连接。

2.2.1 左外连接

image-20240416162417809

语法:

select 字段名 from 表名1 left join 表名2 on 连接条件;

案例:

-- 查询所有同学的信息,没有班级也要显示
select * from student left join class on student.classId = class.classId;

运行结果:

image-20240416161201950

2.2.2 右外连接

image-20240416162556727

语法:

select 字段名 from 表名1 right join 表名2 on 连接条件;

案例:

-- 查询所有班级的信息,没有学生的班级也要显示
select * from student right join class on student.classId = class.classId;

运行结果:

image-20240416161341743

2.3 自连接

自连接是指在同一张表连接自身进行查询。

自连接将行与行之间的关系, 转换为列于列的关系

测试表:

image-20240418203459272

案例: 查询成绩表中的 Java 成绩大于 C语言成绩的同学

image-20240418202430781

为什么直接自连接报错呢? 如何报错的呢? 通过别名的方式来进行自连接

image-20240418202732384

添加连接条件

select * from sore as s1,sore as s2 where s1.name = s2.name and s1.className ='Java' and s2.className = 'C语言' and s1.sore > s2.sore;

运行结果:

image-20240418203408939

3. 子查询

子查询是指嵌入在其他 SQL 语句中的 select语句,也叫嵌套查询

这种写法实际开发中需要慎重使用, 这种写法违背了编程基本的思想原则(化繁为简), 如果是合并之后的 SQL 命令仍然非常简单直观的话, 使用子查询也是可以的.

3.1 单行子查询

单行子查询: 返回一行记录的子查询

案例: 查询张三同学的同班同学

image-20240418204738020

上诉两条 SQL 命令就可以转化为一条命令

select name from student where classId = (select classId from student where name = '张三') and name != '张三';

运行结果:

image-20240418204911546

3.2 多行子查询

多行子查询:返回多行记录的子查询

案例: 查询学习 计算机基础 或者 Python同学的课程信息

在这里插入图片描述

上诉两条 SQL 命令就可以转化为一条命令

select * from sore where name in (select name from sore where className = '计算机基础' or className = 'Python');

运行结果:

在这里插入图片描述

4. 联合查询

在实际应用中,为了合并多个 select 的执行结果,可以使用集合操作符 unionunion all。使用unionunion all时,前后查询的结果集中,字段需要一致。

  • union : 该操作符用于取得两个结果集的并集. 当使用该操作符时,会自动去掉结果集中的重复行。

    案例: 查询成绩小于90 或者 课程为 Java 的信息

    select * from sore where sore < 90 or className = 'Java';
    select * from sore where sore < 90 union select * from sore where className = 'Java';
    

    运行结果:
    在这里插入图片描述

  • union all : 该操作符用于取得两个结果集的并集。当使用该操作符时,不会去掉结果集中的重复行。

    案例: 查询成绩小于90 或者 课程为 Java 的信息

    select * from sore where sore < 90 union all select * from sore where className = 'Java';
    

    在这里插入图片描述

相关推荐

  1. MySQL - 查询

    2024-04-22 15:06:01       64 阅读
  2. MySQL-查询

    2024-04-22 15:06:01       74 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-22 15:06:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-22 15:06:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-22 15:06:01       82 阅读
  4. Python语言-面向对象

    2024-04-22 15:06:01       91 阅读

热门阅读

  1. 蓝桥杯刷题-约数的个数

    2024-04-22 15:06:01       46 阅读
  2. 分布式与集群区别

    2024-04-22 15:06:01       34 阅读
  3. arm-day8

    2024-04-22 15:06:01       30 阅读
  4. spdlog 日志库部分源码说明

    2024-04-22 15:06:01       37 阅读
  5. 软件测试 -- 自动化测试(Selenium)

    2024-04-22 15:06:01       38 阅读
  6. 2023-2024年人形机器人行业报告合集(精选397份)

    2024-04-22 15:06:01       33 阅读
  7. pcb的几种常见认证

    2024-04-22 15:06:01       33 阅读
  8. uniapp 页面跳转通信上下级页面互传

    2024-04-22 15:06:01       33 阅读