首页 > 解决方案 > 在不使用 union all 的情况下获取 union all 的输出

问题描述

我遇到了一种情况,可以在不使用union. 我能行。但我无法做到“联合所有”。我的意思是我必须找出 2 个表的输出,包括重复而不使用union all. 有什么办法吗?

table A has x column and values 1,2,3 
     and
table B has x column and values 3,4,5

select x from A union select x from B;
o/p 1,2,3,4,5

select x from A union all select x from B;
o/p should be 1,2,3,3,4,5,6(not necessarily in order)

Union输出我可以通过下面的查询来实现

select nvl(a.x,b.x) output from A full outer join B on A.x=b.X order by output;

但我union all不能不使用 oracle inbuilt union all

标签: sqloracleoracle11g

解决方案


你离你的答案太近了。

对于union,您使用了以下查询,其中您在列 x 上有一个联接:

select nvl(a.x,b.x) output from A full outer join B on A.x=b.X order by output;

对于union all,您可以使用永远无法满足的连接条件。它将生成与 相同的输出union all

因此,您必须使用以下查询:

select nvl(a.x,b.x) output from A full outer join B on 1=2 order by output;

干杯!!


推荐阅读