2015-11-30 97 views
2

我對sql很新,我一直在嘗試一些運氣很小的東西。這是我的情況。我有表A具有多個鏈接到另一個tableB的:Oracle SQL在同一個表的多個表中選擇null

select * from tableA where id = 1; 
+--+---------+---------+---------+ 
|ID|TABLEB1ID|TABLEB2ID|TABLEB3ID| 
+--+---------+---------+---------+ 
|1 |1  |2  |3  | 
+--+---------+---------+---------+ 

select * from tableB; 
+--+----+ 
|ID|NAME| 
+--+----+ 
|1 |A | 
+--+----+ 
|2 |B | 
+--+----+ 
|3 |C | 
+--+----+ 

現在,我想從每個都對應於這3個ID的行獲取值。現在我在做這樣的事情:

select tableA.*, tableB1.name name1, tableB2.name name2, tableB3.name name3 
from tableA, tableB tableB1, tableB tableB2, tableB tableB3 
where tableA.id = 1 and 
     tableA.tableB1id = tableB1.id and 
     tableA.tableB2id = tableB2.id and 
     tableA.tableB3id = tableB3.id; 
+--+---------+---------+---------+-----+-----+-----+ 
|ID|TABLEB1ID|TABLEB2ID|TABLEB3ID|NAME1|NAME2|NAME3| 
+--+---------+---------+---------+-----+-----+-----+ 
|1 |1  |2  |3  |A |B |C | 
+--+---------+---------+---------+-----+-----+-----+ 

現在這個工作,除了偉大的,當這些ID的一個爲空,我不會得到結果:

select * from tableA where id = 2; 
+--+---------+---------+---------+ 
|ID|TABLEB1ID|TABLEB2ID|TABLEB3ID| 
+--+---------+---------+---------+ 
|2 |1  |2  |   | 
+--+---------+---------+---------+ 

select tableA.*, tableB1.name name1, tableB2.name name2, tableB3.name name3 
from tableA, tableB tableB1, tableB tableB2, tableB tableB3 
where tableA.id = 2 and 
     tableA.tableB1id = tableB1.id and 
     tableA.tableB2id = tableB2.id and 
     tableA.tableB3id = tableB3.id; 

+--+---------+---------+---------+-----+-----+-----+ 
|ID|TABLEB1ID|TABLEB2ID|TABLEB3ID|NAME1|NAME2|NAME3| 
+--+---------+---------+---------+-----+-----+-----+ 
+--+---------+---------+---------+-----+-----+-----+ 

我不是太熟悉,所有的功能,SQL了,而且我希望得到任何幫助在得到像下面的輸出:

+--+---------+---------+---------+-----+-----+-----+ 
|ID|TABLEB1ID|TABLEB2ID|TABLEB3ID|NAME1|NAME2|NAME3| 
+--+---------+---------+---------+-----+-----+-----+ 
|2 |1  |2  |   |A |B |  | 
+--+---------+---------+---------+-----+-----+-----+ 

OR

+--+---------+---------+-----+-----+ 
|ID|TABLEB1ID|TABLEB2ID|NAME1|NAME2| 
+--+---------+---------+-----+-----+ 
|2 |1  |2  |A |B | 
+--+---------+---------+-----+-----+ 

我知道必須有一個可以幫助我的功能,我只是無法找到它並編寫查詢。任何幫助都會很棒。謝謝。

回答

3

您需要使用此方法的LEFT OUTER JOIN。你現在正在做的JOIN的類型相當於INNER JOIN,除非所有連接的雙方都滿意,否則它將不會返回,這就是爲什麼你沒有得到任何結果。

隨着OUTER JOIN,如果數據沒有任何的TableB變異的存在,你就乾脆回去NULL

嘗試以下操作來代替:

Select  A.*, 
      B1.Name Name1, 
      B2.Name Name2, 
      B3.Name Name3 
From  TableA A 
Left Join TableB B1 On A.TableB1ID = B1.Id 
Left Join TableB B2 On A.TableB2ID = B2.Id 
Left Join TableB B3 On A.TableB3ID = B3.Id 
Where  A.Id = 2; 
+0

由於OP'INNER JOIN'是同一個表的3次,而不是上述表的3個不同實例,所以更糟糕。 –

1

到左外的另一起是在選擇列表中使用子查詢:

select t1.*, 
     (select t2.name from tableb t2 where t1.tableb1id = t2.id) name1, 
     (select t3.name from tableb t3 where t1.tableb2id = t3.id) name2, 
     (select t4.name from tableb t4 where t1.tableb3id = t4.id) name3 
from tableA t1; 

     ID TABLEB1ID TABLEB2ID TABLEB3ID NAME1 NAME2 NAME3 
---------- ---------- ---------- ---------- ----- ----- ----- 
     1   1   2   3 A  B  C  
     2   1   2   A  B   

這樣做使子查詢高速緩存的好處,這取決於您的數據是否有許多重複值,可能會也可能不會提供性能優勢。您應該針對您的數據測試兩個查詢,以查看哪一個對您最有效。

+0

這個工程也很棒。不幸的是我不能接受這兩個。多謝你們。 – mrfred