2011-11-17 23 views
0

我有三個表如下三個表:加入使用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?

+0

這是一個家庭作業的問題。請告訴你迄今爲止所嘗試的。 – Tomalak

+0

@Tomalak我嘗試使用select * from table1,table2,table3 where table1.primarykey = table2.primarykey and table2.primarykey = table3.primarykey – epsac

+0

@OMG Ponies我使用(+)但沒有達到我想要的效果。 – epsac

回答

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); 
2
SELECT * 
FROM table1 
LEFT OUTER JOIN table2 ON (table1.name = table2.name) 
LEFT OUTER JOIN table3 ON (table1.name = table3.name)