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 的连接到这里就结束了,有不懂的小可爱可以私聊我,

相关推荐
爱吃提升4 分钟前
Python 使用 MySQL 数据库进行事务处理步骤
数据库·python·mysql
Antoine-zxt4 分钟前
深入解析MySQL事务ACID:从理论到实践的完整指南
数据库·mysql·oracle
7ioik5 分钟前
什么是版本链?
数据库·mysql
手握风云-5 分钟前
MySQL数据库精研之旅第二十期:存储过程,数据处理的全能工具箱(三)
数据库·mysql
2509_9408802210 分钟前
MySQL Workbench菜单汉化为中文
android·数据库·mysql
胖头鱼的鱼缸(尹海文)10 分钟前
数据库管理-第399期 Oracle 19c搭建DG Far Sync日志备库(20260107)
数据库·oracle
Vic1010111 分钟前
华为云高斯数据库:gsqlexec用法
java·大数据·数据库·postgresql·华为云
techdashen19 分钟前
MySQL体系架构 - 简洁版
数据库·mysql·架构
+VX:Fegn089520 分钟前
计算机毕业设计|基于springboot + vue旅游网系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
梁正雄22 分钟前
linux服务-MariaDB 10.6 Galera Cluster 部署
android·数据库·mariadb