OUTER JOIN从不同的列名到同一列

时间:2016-02-21 06:07:53

标签: oracle sqlplus outer-join

我正在尝试从不同的表中选择列(列具有不同的名称),并使用外部联接将它们放在单个表中。我该怎么做呢? (我使用的是sqlplus) 这是一个例子:

表a:

a.NAME1        a.NAME2      a.RATING
Jack           Sparrow      4

表b:

b.FIRSTNAME    b.LASTNAME   b.RATING
Jack           Sparrow      7

表3:

c.F_NAME       c.L_NAME     c.RATING
Jack           Sparrow      6

我想要一张这样的表:

NAME            RATING
Jack            4
                7
                6

我试过这段代码

SELECT
    a.NAME1 AS NAME,
    b.FIRSTNAME AS NAME,
    c.F_NAME AS NAME,
    a.RATING AS RATING,
    b.RATING AS RATING,
    c.RATING AS RATING
FROM a
FULL OUTER JOIN (b
CROSS JOIN c)
    ON (a.NAME1 = b.FIRSTNAME
    AND a.NAME1 = c.F_NAME);

但那没用。我该如何实现这一目标?

2 个答案:

答案 0 :(得分:2)

听起来你根本不想加入桌子。如果您连接了三个表,每个表有一行,那么最终会得到一个包含单行和多列的结果集。由于您的目标是最终得到三行数据,因此您需要使用union all

SELECT a.name1, a.rating 
  FROM a
UNION ALL
SELECT b.firstname, b.rating 
  FROM b
UNION ALL
SELECT c.f_name, c.rating 
  FROM c

如果您想消除重复的行,请使用union而不是union all

答案 1 :(得分:0)

select a.NAME1, a.NAME2, a.RATING, b.RATING, c.RATING
from a 
left outer join b on b.FIRSTNAME = a.NAME1 and b.LASTNAME = a.NAME2
left outer join c on c.F_NAME = a.NAME1 and c.L_NAME = a.NAME2