我对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个不同实例,所以更糟糕。 –