内连接查询与外连接查询

2022-10-26 09:26:13

内连接查询与外连接查询

示例用表
type字段部分数据相同
1.内连接:[inner] join
基本语法:左表 [inner] join 右表 on 左表.字段 = 右表.字段;
示例用法
select * from A inner join B on A.type = B.type;
示例结果(黄色部分)
在这里插入图片描述
2.外连接:以某张表为主,取出里面的所有记录, 然后每条与另外一张表进行连接: 不管能不能匹配上条件,最终都会保留: 能匹配,正确保留; 不能匹配,其他表的字段都置空NULL。

2.1左(外)连接:left join: 以左表为主表
基本语法: from 左表 left join 右表 on 左表.字段 = 右表.字段;
示例用法
select * from A left join B on A.type = B.type;
示例结果(黄色部分,无数据为null)
在这里插入图片描述
2.2右(外)连接:right join: 以右表为主表
基本语法: from 左表 right join 右表 on 左表.字段 = 右表.字段;
示例用法
select * from A right join B on A.type = B.type;
示例结果(黄色部分,无数据为null)
在这里插入图片描述
2.3全(外)连接
基本语法: from 左表 full join 右表 on 左表.字段 = 右表.字段;
示例用法
select * from A full join B on A.type = B.type;
示例结果(黄色部分,无数据为null)
在这里插入图片描述

  • 作者:ForestProsperity
  • 原文链接:https://blog.csdn.net/Kami_Lin/article/details/109055212
    更新时间:2022-10-26 09:26:13