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

相关推荐
橘猫云计算机设计10 分钟前
基于Java的班级事务管理系统(源码+lw+部署文档+讲解),源码可白嫖!
java·开发语言·数据库·spring boot·微信小程序·小程序·毕业设计
多多*15 分钟前
JavaEE企业级开发 延迟双删+版本号机制(乐观锁) 事务保证redis和mysql的数据一致性 示例
java·运维·数据库·redis·mysql·java-ee·wpf
酷爱码37 分钟前
数据库索引相关的面试题以及答案
数据库
以待成追忆41 分钟前
Scrapy——Redis空闲超时关闭扩展
数据库·redis·scrapy
转转技术团队1 小时前
"慢SQL"治理的几点思考
数据库·mysql·性能优化
bingbingyihao1 小时前
Windows下安装常用软件--MySQL篇
windows·mysql
m0_653031362 小时前
加新题了,MySQL 8.0 OCP 认证考试 题库更新
数据库·mysql·开闭原则
hrrrrb2 小时前
【MySQL】锁机制
数据库·mysql
PacosonSWJTU2 小时前
python基础-07-模式匹配与正则表达式
python·mysql·正则表达式