Oracle SQL在同一表的多个表上选择null
我对sql非常陌生,我一直在尝试一些运气很差的东西。这是我的情况。我有一个tableA,它有多个指向另一个tableB的链接:Oracle SQL在同一表的多个表上选择null,sql,oracle,Sql,Oracle,我对sql非常陌生,我一直在尝试一些运气很差的东西。这是我的情况。我有一个tableA,它有多个指向另一个tableB的链接: select * from tableA where id = 1; +--+---------+---------+---------+ |ID|TABLEB1ID|TABLEB2ID|TABLEB3ID| +--+---------+---------+---------+ |1 |1 |2 |3 | +--+-------
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为null时,我没有得到任何结果:
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 | |
+--+---------+---------+---------+-----+-----+-----+
或
我知道必须有一个函数可以帮助我解决这个问题,我只是在查找和编写查询时遇到了困难。任何帮助都会很好。谢谢。这种方法需要使用
左外连接。您现在正在执行的连接的类型相当于内部连接
,它不会返回,除非所有连接的两侧都满足,这就是您没有得到任何结果的原因
使用外部联接
,如果任何表b
变体的数据都不存在,您只需返回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;
左外部联接的替代方法是在选择列表中使用子查询:
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
这样做的好处是启用子查询缓存,根据数据是否有多个重复值,子查询缓存可能提供性能优势,也可能不提供性能优势。您应该针对您的数据测试这两个查询,看看哪一个对您最合适。更糟糕的是,OP内部联接将同一个表重复了3次,而不是所述表的3个不同实例。这也非常有效。不幸的是,我不能两者都接受。谢谢各位。
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