我對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 |
+--+---------+---------+-----+-----+
我知道必須有一個可以幫助我的功能,我只是無法找到它並編寫查詢。任何幫助都會很棒。謝謝。
由於OP'INNER JOIN'是同一個表的3次,而不是上述表的3個不同實例,所以更糟糕。 –