在具有多个 ID 的同一 table 上左连接两次
Left join twice on same table with multiple ids
我如何在 1 table 上连接两次,然后将它连接到另一个 table 的 2 个不同列。
SELECT table1.id_a, table2.INFO AS Data_1, table1.id_b, table2.INFO AS Data_2
FROM table1
LEFT OUTER JOIN table2 ON table2.id_a = table1.id_a
LEFT OUTER JOIN table2 ON table2.id_a = table1.id_b;
Data1 列应显示对应于 id_a 的信息
和
Data2 列应显示对应于 id_b
的信息
TABLE 1
--------
ID_A ID_B
------------------
A X
B Y
C Z
TABLE 2
--------
ID_A INFO
-----------------
A apple
B pear
C orange
Y watermelon
X banana
Z blueberry
Expected output
--------------
ID_A Data1 ID_B Data2
-----------------------------------------------
A apple X watermelon
B pear Y banana
C orange Z blueberry
在没有更多信息的情况下您的查询是否会实现您想要的结果对我来说并不明显,但我确定您当前的问题是缺少别名。为每个 table 设置别名,以便您可以明确引用每个:
SELECT t1.id_a, t2a.INFO AS Data_1, t1.id_b, t2b.INFO AS Data_2
FROM table1 t1
LEFT OUTER JOIN table2 t2a ON t2a.id_a = t1.id_a
LEFT OUTER JOIN table2 t2b ON t2b.id_a = t1.id_b;
编辑:充分展示:
with tab1 as (
select 'A' id_a, 'X' id_b from dual
union all
select 'B' id_a, 'Y' id_b from dual
union all
select 'C' id_a, 'Z' id_b from dual)
, tab2 as (
SELECT 'A' id_a, 'apple' info from dual
union all
SELECT 'B' id_a, 'pear' info from dual
union all
SELECT 'C' id_a, 'orange' info from dual
union all
SELECT 'X' id_a, 'watermelon' info from dual
union all
SELECT 'Y' id_a, 'banana' info from dual
union all
SELECT 'Z' id_a, 'blueberry' info from dual
)
select t1.id_a, t2.info, t1.id_b, t3.info
from tab1 t1
left outer join tab2 t2 on t2.id_a = t1.id_a
left outer join tab2 t3 on t3.id_a = t1.id_b;
ID_A INFO ID_B INFO_1
A apple X watermelon
B pear Y banana
C orange Z blueberry
我如何在 1 table 上连接两次,然后将它连接到另一个 table 的 2 个不同列。
SELECT table1.id_a, table2.INFO AS Data_1, table1.id_b, table2.INFO AS Data_2
FROM table1
LEFT OUTER JOIN table2 ON table2.id_a = table1.id_a
LEFT OUTER JOIN table2 ON table2.id_a = table1.id_b;
Data1 列应显示对应于 id_a 的信息 和 Data2 列应显示对应于 id_b
的信息TABLE 1
--------
ID_A ID_B
------------------
A X
B Y
C Z
TABLE 2
--------
ID_A INFO
-----------------
A apple
B pear
C orange
Y watermelon
X banana
Z blueberry
Expected output
--------------
ID_A Data1 ID_B Data2
-----------------------------------------------
A apple X watermelon
B pear Y banana
C orange Z blueberry
在没有更多信息的情况下您的查询是否会实现您想要的结果对我来说并不明显,但我确定您当前的问题是缺少别名。为每个 table 设置别名,以便您可以明确引用每个:
SELECT t1.id_a, t2a.INFO AS Data_1, t1.id_b, t2b.INFO AS Data_2
FROM table1 t1
LEFT OUTER JOIN table2 t2a ON t2a.id_a = t1.id_a
LEFT OUTER JOIN table2 t2b ON t2b.id_a = t1.id_b;
编辑:充分展示:
with tab1 as (
select 'A' id_a, 'X' id_b from dual
union all
select 'B' id_a, 'Y' id_b from dual
union all
select 'C' id_a, 'Z' id_b from dual)
, tab2 as (
SELECT 'A' id_a, 'apple' info from dual
union all
SELECT 'B' id_a, 'pear' info from dual
union all
SELECT 'C' id_a, 'orange' info from dual
union all
SELECT 'X' id_a, 'watermelon' info from dual
union all
SELECT 'Y' id_a, 'banana' info from dual
union all
SELECT 'Z' id_a, 'blueberry' info from dual
)
select t1.id_a, t2.info, t1.id_b, t3.info
from tab1 t1
left outer join tab2 t2 on t2.id_a = t1.id_a
left outer join tab2 t3 on t3.id_a = t1.id_b;
ID_A INFO ID_B INFO_1
A apple X watermelon
B pear Y banana
C orange Z blueberry