在数据库中建立两张表,分别是 user 和 u
user表:

u表

关联字段user.u_id 和 u.parent_id来演示一下MySQL的 内连接、左连接(左外连接)、右连接(右外连接)、全连接(全外连接)
内连接
- 关键字:inner join on
- sql语句:
select user.*,u.* from user inner join u on user.u_id=u.parent_id - 运行结果:
- 分析:
内连接就是找两个表的交集
- 分析:
左连接(左外连接)
- 关键字:left join on
- sql语句:
select user.*,u.* from user left join u on user.u_id=u.parent_id - 运行结果:

- 分析
左连接是以左边的表的数据为主,匹配右边表的数据。
右连接(右外连接)
-
关键字:right join on
-
sql语句:
select user.*,u.* from user right join u on user.u_id=u.parent_id -
运行结果:

-
分析
左连接是以右边的表的数据为主,匹配左边表的数据。
全连接(全外连接)
- 关键字:full join on
- sql语句:
select user.*,u.* from user full join u on user.u_id=u.parent_id
mysql不支持全连接!!!
但是!!!可以用别的方法实现全连接
使用左连接 结合 右连接 实现 全连接!!!

本文深入解析了MySQL中四种主要的表连接方式:内连接、左连接、右连接及全连接的实现方法与应用场景,通过具体SQL语句示例,帮助读者理解不同连接方式下数据的匹配逻辑。
、右连接(右外连接)、全连接(全外连接)&spm=1001.2101.3001.5002&articleId=107246395&d=1&t=3&u=e1ea066f5dde4ec294643f44ae7d9f90)
3875

被折叠的 条评论
为什么被折叠?



