使用Oracle加入三个表

时间:2011-11-17 05:36:09

标签: sql oracle

我有三张表如下:

table 1          table 2          table 3
-------          -------          -------
a                a                a
b                c                c
c                f                
d
e
f

我想将这三个表加入到1中,这将产生以下结果:

result table
------------

a               a                a
b
c               c                c
d
e
f               f

注意到第二个和第三个col如果没有匹配则包含空行。如何使用oracle sql实现这一目标?

2 个答案:

答案 0 :(得分:2)

SELECT *
FROM table1
LEFT OUTER JOIN table2 ON ( table1.name = table2.name )
LEFT OUTER JOIN table3 ON ( table1.name = table3.name )

答案 1 :(得分:0)

 -- Enhance table
 alter table table1 add (field2 /*e.g.*/ varchar2(10)
                        ,field3 /*e.g.*/ varchar2(10) );

 -- update rows roughly works like this. (I don't exactly know your column names, primary keys, etc)
 update table1 o set o.field2 = (select i.field from table2
                                 where o.field1 = i.field);