连接具有多行和相同列名的表

时间:2018-04-16 10:32:38

标签: sql oracle union union-all

表1

id | name | gender  
1  | ABC  |  M  
2  | CDE  |  M  
3  | FGH  |  M  


表2

id | name | gender  
4  | BAC  |  F  
5  | DCE  |  F  
6  | GFH  |  F  


如何在oracle数据库中输出如下:

id | name | gender  
1  | ABC  |  M  
2  | CDE  |  M  
3  | FGH  |  M  
4  | BAC  |  F  
5  | DCE  |  F  
6  | GFH  |  F  

2 个答案:

答案 0 :(得分:3)

使用2017-11

public static void SAPLogger(string Message)
        {

            TelemetryConfiguration.Active.InstrumentationKey = "XXX-XXX-XXX";
            TelemetryClient TelePositive = new TelemetryClient
            {
                InstrumentationKey = "XXX-XXX" (Optional Value)
            };


            //TelePositive.TrackRequest(Req);
            TelePositive.TrackTrace(Message, SeverityLevel.Verbose, new Dictionary<string, string> { { "Information", "SAP" } });

        }

P.S。如果单个static void Main(string[] args) { try { int a = 5; int c = a / 2; SAPLogger("The value is Success" + c); } } 语句存在任何重复的行,UNION [ALL]将删除重复项,但select * from table1 union all select * from table2; 连接行,即使它们是重复的。

答案 1 :(得分:1)

如果你真的需要加入&#34; 2桌:

with a as (
  select 1 id, 'ABC' name, 'M' gender from dual union all
  select 2 id, 'CDE' name, 'M' gender from dual union all
  select 3 id, 'FGH' name, 'M' gender from dual ), 
b as (
  select 4 id, 'BAC' name, 'F' gender from dual union all
  select 5 id, 'DCE' name, 'F' gender from dual union all
  select 6 id, 'GFH' name, 'F' gender from dual )
select coalesce(a.id, b.id) id,
       coalesce(a.name, b.name) name,
       coalesce(a.gender, b.gender) gender
  from a
  full join b
    on a.id = b.id
    /* if name, gender not in pk */
--   and a.name = b.name
--   and a.gender = b.gender
;

在这种情况下,将删除所有重复的&#34; ID&#34; s。首先不是&#34;名称&#34;,&#34;性别&#34;列将由coalesce函数返回。

您甚至可以使用greatestleast和ets,而不是合并..

<强> P.S。如果你没有PK,请小心!