MySQL数据库第十课-------join连接的再续------强强连锁

作者前言

欢迎小可爱们前来借鉴我的gtiee秦老大大 (qin-laoda) - Gitee.com


目录

join连接

内连接

左连接

右连接

外连接

其他连接


作者小插曲

最近学校课程很多,无法及时更新,还望各位小可爱多多包含,你的三连是我的动力

插播小知识

1.当我们拼接的时候,有些表的字段数不够就用null补上

内连接

sql 复制代码
select  
*
from 
(select * from new_employees limit 10)as ne
join 
(select * from employees limit 10)as em
on ne.emp_no=em.emp_no;

左连接

sql 复制代码
select  
*
from 
(select * from new_employees limit 20)as ne
left join 
(select * from employees limit 10)as em
on ne.emp_no=em.emp_no;

右连接

sql 复制代码
select  
*
from 
(select * from new_employees limit 10)as ne
right join 
(select * from employees limit 20)as em
on ne.emp_no=em.emp_no;

外连接

sql 复制代码
select  
*
from 
(select * from new_employees limit 20)as ne
left join 
(select * from employees limit 10)as em
on ne.emp_no=em.emp_no
union 
select  
*
from 
(select * from new_employees limit 10)as ne
right join 
(select * from employees limit 20)as em
on ne.emp_no=em.emp_no;

这里的代码我就不使用图片表示了,在上一篇的关于jion 的这些讲了很清楚了,现在我们来接着下面来

其他连接

其他连接一:

可以看出在这个连接是取左连接的匹配null部分,我们的思路就是左连接后再筛选出来,

sql 复制代码
select  
ne.emp_no,

ne.`age`,
ne.`gender`,
em.`birth_date`,
em.`first_name`,
em.`last_name`,
em.`gender`,
em.`hire_date`
from 
(select * from new_employees limit 20)as ne
left join 
(select * from employees limit 10)as em
on ne.emp_no=em.emp_no
where em.`emp_no` is null;

结果:

同理

其他连接二:

sql 复制代码
select  
ne.emp_no,
ne.`age`,
ne.`gender`,
em.`birth_date`,
em.`first_name`,
em.`last_name`,
em.`gender`,
em.`hire_date`
from 
(select * from new_employees limit 10)as ne
right join 
(select * from employees limit 20)as em
on ne.emp_no=em.emp_no
where ne.`emp_no` is null;

其他连接3:

思路就是外连接取含有null的部分,先左连接表格,然后筛选出null的部分,再右连接,筛选出null的部分,然后利用union distinct拼接表格,

sql 复制代码
select  
*
from 
(select * from new_employees limit 20)as ne
left join 
(select * from employees limit 10)as em
on ne.emp_no=em.emp_no
where em.emp_no is null
union 
select  
*
from 
(select * from new_employees limit 10)as ne
right join 
(select * from employees limit 20)as em
on ne.emp_no=em.emp_no
where ne.emp_no is null;

注意一下,左连接和右连接都是要有条件筛选的

自关联

把竖表变横表

简单的说就是自己跟自己关联

这张表的内容

这张表自连接

复制代码
select * from 
city as a
join
city  as b
on a.`id` = b. pid;

子查询

在一个 select 语句中,嵌入了另一个 select 语句,前面我写的时候就用到过了

复制代码
select 
*
from
(select * from city limit 10) as a;
sql 复制代码
-- 标量子查询
-- 查询大于等于公司平均年龄的员工
select * from table
where age >= (select avg(age) from table);
-- 列级子查询
select * from table1
where id in (select id from table2);
-- 行级子查询
select * from table
where (height,age) = (select max(height),max(age) from table);

上述写法是没问题的但是,这样写不怎么好,一般我们可以利用join 来连接

sql 复制代码
 select 
 *
 from new_employees as b
join 
 (
    select avg(age) as av from new_employees 
 )as a 
 on b.age>= a.av
 limit 10;

总结:


执行顺序为:
from 表名
join
where
group by
select distinct *
having
order by
limit start, coun

join on中的on 是对主表和附表进行筛选,然后开辟一块一定空间的内存,存放在内存中,where是对join on后的数据进行筛选,

筛选完分组,分完组之后可以说是字段基本是定型了

上面三种子查询就是也可能会在第二个select 的数据存储可能会挤压第一个select开辟的空间造成数据的混乱,,数据只能减少,不能增加

可以理解为先圈选范围再计算数据

总结:

join 的连接到这里就结束了,有不懂的小可爱可以私聊我,

相关推荐
LucianaiB41 分钟前
【金仓数据库征文】_AI 赋能数据库运维:金仓KES的智能化未来
运维·数据库·人工智能·金仓数据库 2025 征文·数据库平替用金仓
时序数据说1 小时前
时序数据库IoTDB在航空航天领域的解决方案
大数据·数据库·时序数据库·iotdb
.生产的驴1 小时前
SpringBoot 封装统一API返回格式对象 标准化开发 请求封装 统一格式处理
java·数据库·spring boot·后端·spring·eclipse·maven
AnsenZhu1 小时前
2025年Redis分片存储性能优化指南
数据库·redis·性能优化·分片
Johny_Zhao2 小时前
MySQL 高可用集群搭建部署
linux·人工智能·mysql·信息安全·云计算·shell·yum源·系统运维·itsm
oydcm2 小时前
MySQL数据库概述
数据库·mysql
oioihoii2 小时前
C++23中if consteval / if not consteval (P1938R3) 详解
java·数据库·c++23
带娃的IT创业者2 小时前
《AI大模型趣味实战》基于RAG向量数据库的知识库AI问答助手设计与实现
数据库·人工智能
husterlichf3 小时前
MYSQL 常用数值函数 和 条件函数 详解
数据库·sql·mysql
我的golang之路果然有问题3 小时前
快速了解redis,个人笔记
数据库·经验分享·redis·笔记·学习·缓存·内存