如何将不同表的多列合并到一个表中

时间:2018-07-11 10:49:16

标签: sql postgresql

我有三个表a,b和c,需要将这些表数据安排为目标表,并且所有这些表(a,b,c)都不在数据库中,它们是使用查询作为别名从单个表中获取的,需要使用查询将这些表安排到目标表中。怎么做

   table a     |      table b     |   table c     
               |                  |               
  id | a_vals  |     id | b_vals  |  id | c_vals  
  ------------ | --------------   | ------------- 
  1  |  123    |     1  |  123    |  1  |  123    
  2  |  124    |     2  |  142    |  2  |  142    
  3  |  234    |     4  |  234    |  5  |  234    

目标表

id | a_val| b_val| c_val
1  | 123  | 123  | 123
2  | 124  | 142  | 142
3  | 234  |  -   |  -
4  |  -   | 234  |  -
5  |  -   |      | 234

5 个答案:

答案 0 :(得分:1)

由于a,b和c为要连接的列共享相同的名称,因此可以使用“ USING”来避免结果表中的重复键:

SELECT *
FROM a
FULL OUTER JOIN b USING (id)
  FULL OUTER JOIN c USING (id);

或者,由于a,b和c的value列都具有不同的名称,因此您可以使用NATURAL JOIN:

SELECT *
FROM a
NATURAL FULL OUTER JOIN b
NATURAL FULL OUTER JOIN c;

请注意不要意外重命名其他任何列,因为自然连接会删除任何重复的列。 如果愿意,还可以省略“ OUTER”关键字,但是为了清楚起见,我将其保留下来(因为LEFT,RIGHT和FULL表示外部联接)。 有关详情,请参见array#splice

答案 1 :(得分:0)

您正在寻找ANSI标准FULL OUTER JOIN

select coalesce(a.id, b.id, c.id) as id, a.val, b.val, c.val
from a full join
     b
     on a.id = b.id full join
     c
     on c.id = coalesce(a.id, b.id);

您还可以使用union all / group by来实现此目的:

select id, max(a_val) as a_val, max(b_val) as b_val, max(c_val) as c_val
from ((select id, val as a_val, null as b_val, null as c_val
       from a
      ) union all
      (select id, null as a_val, val as b_val, null as c_val
       from b
      ) union all
      (select id, null as a_val, null as b_val, val as c_val
       from c
      )
     ) abc
group by id;

答案 2 :(得分:0)

尝试此代码

SELECT 
CASE
    WHEN t1.id IS not null THEN t1.id
    WHEN t2.id IS not null THEN t2.id  
    ELSE t3.id
  END 
  AS id,
t1.a_vals AS a_val, 
t2.b_vals as b_val, 
t3.c_vals as c_val
FROM a t1  FULL OUTER JOIN b t2 ON t1.id=t2.id FULL OUTER JOIN c t3 ON 
CASE
    WHEN t1.id IS not null THEN t1.id
    ELSE t2.id
  END = t3.id

OR

SELECT  COALESCE(t1.id, t2.id, t3.id) as id ,
            t1.a_vals AS a_val, 
            t2.b_vals as b_val, 
            t3.c_vals as c_val
    FROM a t1 FULL OUTER JOIN b t2 ON t1.id=t2.id 
    FULL OUTER JOIN c t3 ON COALESCE(t1.id, t2.id) = t3.id

答案 3 :(得分:0)

请尝试以下操作:

 select aa.id, a_val, b_val, c_val from 
 (select distinct id as id from table_a
 union
 select distinct id as id from table_b
 union
 select distinct id as id from table_c)aa
 left join (select id, a_val from table_a)bb on aa.id = bb.id
 left join (select id, b_val from table_b)cc on aa.id = cc.id
 left join (select id, c_val from table_c)dd on aa.id = dd.id order by aa.id;

答案 4 :(得分:0)

这可能最好在“前端”中完成,例如这就是报表工具的目的。

避免空值和外部联接(因为按定义它们会产生空值):

SELECT a_val, b_val, c_val
  FROM a 
       NATURAL JOIN b 
       NATURAL JOIN c
UNION
SELECT a_val, '-' AS  b_val, '-' AS c_val
  FROM a 
 WHERE id NOT IN ( SELECT id FROM b )
       AND id NOT IN ( SELECT id FROM c )
UNION
SELECT '-' AS a_val, b_val, '-' AS c_val
  FROM b
 WHERE id NOT IN ( SELECT id FROM a )
       AND id NOT IN ( SELECT id FROM c )
UNION
SELECT '-' AS a_val, '-' AS  b_val, c_val
  FROM c
 WHERE id NOT IN ( SELECT id FROM a )
       AND id NOT IN ( SELECT id FROM b );