外链论坛

 找回密码
 立即注册
搜索
查看: 56|回复: 2

SQL语句之内外连接inner join、full join、left join、right join

[复制链接]

2790

主题

6292

回帖

9996万

积分

论坛元老

Rank: 8Rank: 8

积分
99969942
发表于 2024-8-4 10:32:40 | 显示全部楼层 |阅读模式

1,Join:SQL查找中语句中重要的操作之一,用于把来自两个或多个表结合起来,生成新的关系表。常用的Join类型有:内连接(Inner Join)和外连接(outer join),外连接又分为:左连接(Left Join)、右连接(Right Join)和全连接(Full Join)。

2,实验环境数据:table1和table2两个表,要求是字段table1_id和table2_id相等。

table1

table2

3,内连接inner join:即交集,table1和table2中都有的数据。要求是:table1_id和table2_id字段相等的数据。

select table1.*, table2.* from table1 inner join table2 on table1.table1_id=table2.table2_id

inner join

inner join

4,左连接left join返回左表的所有行,且右表与左表完全匹配的右表行,其它匹配不上的为NULL

select table1.*, table2.* from table1 left join table2 on table1.table1_id=table2.table2_id

left join

left join

5,右连接right join返回右表的所有行,且右表与左表完全匹配的左表行,其它匹配不上的为NULL

select table1.*, table2.* from table1 right join table2 on table1.table1_id=table2.table2_id

right join

right join

5,全连接full join:返回两个表各自的匹配或匹配不上的所有行,匹配不上的为NULL

select table1.*, table2.* from table1 full join table2 on table1.table1_id=table2.table2_id

full join

full join

备注:外连接的“OUTER”关键字能够省略不写(如left outer join = left join);内连接只会返回满足连接要求的记录,而外连接则是内连接的扩展,它不仅会返回满足要求的记录,况且还会返回不满足连接要求的记录。

回复

使用道具 举报

0

主题

1万

回帖

1

积分

新手上路

Rank: 1

积分
1
发表于 2024-8-24 18:41:35 | 显示全部楼层
“NB”(牛×的缩写,表示叹为观止)‌
回复

使用道具 举报

0

主题

1万

回帖

1

积分

新手上路

Rank: 1

积分
1
发表于 2024-9-8 03:50:32 | 显示全部楼层
这夸赞甜到心里,让我感觉温暖无比。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

站点统计|Archiver|手机版|小黑屋|外链论坛 ( 非经营性网站 )|网站地图

GMT+8, 2024-10-6 02:28 , Processed in 0.063464 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.