Oracle - 如何将多张表中的列合并到一张表中

在这里插入图片描述

一.问题回顾

在这里插入图片描述

我们目前有表A和表B,两个表分别有一列,我们想查询出来的结果如表C,它同时包含了表A和表B的列;

二.解决方案

为了测试方便,我们直接使用Oracle数据库的scott用户下的表emp和表dept;

表emp:

select rownum as rn1, t.* from scott.emp t

在这里插入图片描述

表dept:

select rownum as rn2, t.* from scott.dept t

在这里插入图片描述

with a as
 (select rownum as rn1, t.* from scott.emp t),
b as
 (select rownum as rn2, t.* from scott.dept t)
select a.*, b.* from a full join b on a.rn1 = b.rn2

在这里插入图片描述

三.归纳总结

大家可以发现,我们使用full join (全连接)来实现我们的需求;

3.1 基本语法

full join ... on ...

3.2 结果

全连接的查询结果是左外连接和右外连接的并集,即使一些记录关联不上,也能够把信息查询出来;

发布了142 篇原创文章 · 获赞 160 · 访问量 22万+

猜你喜欢

转载自blog.csdn.net/Milogenius/article/details/102816404