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

相关推荐
chengooooooo1 分钟前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
Rverdoser1 小时前
【SQL】多表查询案例
数据库·sql
Galeoto1 小时前
how to export a table in sqlite, and import into another
数据库·sqlite
人间打气筒(Ada)2 小时前
MySQL主从架构
服务器·数据库·mysql
leegong231112 小时前
学习PostgreSQL专家认证
数据库·学习·postgresql
喝醉酒的小白2 小时前
PostgreSQL:更新字段慢
数据库·postgresql
敲敲敲-敲代码2 小时前
【SQL实验】触发器
数据库·笔记·sql
和道一文字yyds2 小时前
MySQL 中的索引数量是否越多越好?为什么?如何使用 MySQL 的 EXPLAIN 语句进行查询分析?MySQL 中如何进行 SQL 调优?
数据库·sql·mysql
落笔画忧愁e2 小时前
FastGPT快速将消息发送至飞书
服务器·数据库·飞书
Σίσυφος19003 小时前
halcon 条形码、二维码识别、opencv识别
前端·数据库