数据库四种关联方式

一、关联方式区别与联系

数据库关联方式: 内连接、左外连接、右外连接、全外连接。
内连接(inner join 其中inner可省略 ):展示关联成功的数据
左外连接(left join):以左表为主,左表数据全部展示,右表只展示关联成功的数据,关联失败的显示 /N
右外连接(right join ):以右表为主,右表数据全部展示,左表只展示关联成功的数据,关联失败的显示 /N
全外连接(full outer join):两张表的数据都展示,其中关联失败的展示 /N
联系:
当两张表关联时,两张表数据都能关联成功且无重复时,几种关联方式结果相同
区别:
1、两张表关联失败的数据会影响结果,以左连接为例,关联成功的数据都展示,关联失败的数据只展示左表,右边补空值。
2、关联成功时,重复数据影响关联结果,以左连接,右表有重复数据为例,关联后,数据会翻倍

二、实例演示

准备工作:

--创建测试表
create table test_yw(
	stu_id string COMMENT '学号',
	stu_name string comment '姓名',
	stu_score int COMMENT '语文成绩'
)
;
create table test_sx(
	stu_id string COMMENT '学号',
	stu_name string comment '姓名',
	stu_score int COMMENT '语文成绩'
)
;
--插入测试数据,stu_name 插入重复数据,便于展示数据关联出多条的情况
INSERT into test_yw
VALUES 
('2023001','赵敏','149')
,('2023002','赵敏','139')
,('2023003','乔峰','129')
;
INSERT into test_sx
VALUES 
('2023001','赵敏','119')
,('2023002','赵敏','121')
,('2023004','令狐冲','148')
;

2.1左外连接:
用学号做左关联条件,其中2023003关联失败,右表补空值

SELECT a.stu_id,a.stu_name,a.stu_score,b.stu_id,b.stu_name,b.stu_score
from test_yw a 
LEFT join test_sx b 
on a.stu_id = b.stu_id 
;

查询结果:

stu_id stu_name stu_score stu_id2 stu_name2 stu_score2
2023001 赵敏 149 2023001 赵敏 119
2023002 赵敏 139 2023002 赵敏 121
2023003 乔峰 129 \N \N \N

2.2右外连接
用学号做右关联条件,其中2023004关联失败,右表补空值

SELECT a.stu_id,a.stu_name,a.stu_score,b.stu_id,b.stu_name,b.stu_score
from test_yw a 
right join test_sx b 
on a.stu_id = b.stu_id 
;

查询结果:

stu_id stu_name stu_score stu_id2 stu_name2 stu_score2
2023001 赵敏 149 2023001 赵敏 119
2023002 赵敏 139 2023002 赵敏 121
\N \N \N 2023004 令狐冲 148

2.3全外连接
用学号做关联条件,其中2023004、2023004关联失败,补空值

SELECT a.stu_id,a.stu_name,a.stu_score,b.stu_id,b.stu_name,b.stu_score
from test_yw a 
full OUTER  join test_sx b 
on a.stu_id = b.stu_id 
;

查询结果:

stu_id stu_name stu_score stu_id2 stu_name2 stu_score2
2023001 赵敏 149 2023001 赵敏 119
2023002 赵敏 139 2023002 赵敏 121
2023003 乔峰 129 \N \N \N
\N \N \N 2023004 令狐冲 148

2.4内连接
用学号做关联条件,其中2023004、2023004关联失败,不展示,内连接只展示关联成功的数据2023001、2023002,

SELECT a.stu_id,a.stu_name,a.stu_score,b.stu_id,b.stu_name,b.stu_score
from test_yw a 
(inner) --inner 可以省略
join test_sx b 
on a.stu_id = b.stu_id 
;

查询结果:

stu_id stu_name stu_score stu_id2 stu_name2 stu_score2
2023001 赵敏 149 2023001 赵敏 119
2023002 赵敏 139 2023002 赵敏 121

注:
当数据存在重复情况时,关联时应考虑数据翻倍情况。以全外连接为例,当关联条件由学号改为姓名时,数据关联后,会展示多条

SELECT a.stu_id,a.stu_name,a.stu_score,b.stu_id,b.stu_name,b.stu_score
from test_yw a 
full outer join test_sx b 
-- on a.stu_id = b.stu_id 
on a.stu_name = b.stu_name
;

stu_id stu_name stu_score stu_id2 stu_name2 stu_score2
2023003 乔峰 129 \N \N \N
\N \N \N 2023004 令狐冲 148
2023001 赵敏 149 2023001 赵敏 119
2023001 赵敏 149 2023002 赵敏 121
2023002 赵敏 139 2023001 赵敏 119
2023002 赵敏 139 2023002 赵敏 121

说明:
左表、右表姓名为赵敏的有两条,关联时
左表 2023001 2023001 赵敏 149 关联 右表 2023001 赵敏 119
再关联右表 2023002 赵敏 121,展示两条,同理左表2023002的赵敏,也会关联右表2023001/2023002的赵敏,展示两条,导致原本左表、右表各两条的,会生成22=4条数据(左表数据右表数据)
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值