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

相关推荐
wrx繁星点点7 分钟前
事务的四大特性(ACID)
java·开发语言·数据库
小小娥子36 分钟前
Redis的基础认识与在ubuntu上的安装教程
java·数据库·redis·缓存
DieSnowK38 分钟前
[Redis][集群][下]详细讲解
数据库·redis·分布式·缓存·集群·高可用·新手向
计算机学姐1 小时前
基于SpringBoot+Vue的高校运动会管理系统
java·vue.js·spring boot·后端·mysql·intellij-idea·mybatis
-XWB-1 小时前
【MySQL】数据目录迁移
数据库·mysql
老华带你飞1 小时前
公寓管理系统|SprinBoot+vue夕阳红公寓管理系统(源码+数据库+文档)
java·前端·javascript·数据库·vue.js·spring boot·课程设计
我明天再来学Web渗透2 小时前
【hot100-java】【二叉树的层序遍历】
java·开发语言·数据库·sql·算法·排序算法
Data 3172 小时前
Hive数仓操作(十一)
大数据·数据库·数据仓库·hive·hadoop
吱吱鼠叔2 小时前
MATLAB数据文件读写:2.矩阵数据读取
数据库·matlab·矩阵
掘根2 小时前
【MySQL】Ubuntu环境下MySQL的安装与卸载
数据库·mysql·centos