1

我需要帮助才能加入三张桌子。不知何故,我可以加入两个表table1table2获得所需的输出,但我想加入另一个表以获得一些新的相关列。这是我的查询:

select 
[from_bus] as [Node], 
[from_bus_id]  as [Node_id]
from table1 
union 
select 
[to_bus] as [Node], 
[to_bus_id]  as [Node_id]
from table1
union
select 
[from_bus] as [Node], 
[from_bus_id]  as [Node_id]
from table2
union 
select 
[to_bus] as [Node], 
[to_bus_id]  as [Node_id]
from table2

从 table1 和 table2 查询输出:

Node Node_ID
A_22  1
A_11  2
B_33  3
C_25  4

Node 和 Node_ID 是唯一的。现在,我有另一个 table3,我需要另一个(Zone_ref)包含IDs相应Zone.

    table3: 
    Zone   Node_Name  Zone_ref
    A      A_22        1
    A      A_11        1
    B      B_33        3 
    B      B_44        3     
    C      C_31        4
    C      C_25        4

我想要类似的东西:

Node Node_ID Zone_ref
A_22  1       1
A_11  2       1
B_33  3       3
C_25  4       4

有一些常见的字段我可以加入表,但不知道如何将两个表中的一个查询与第三个表集成。需要你的建议。顺便说一句,我正在使用访问数据库。谢谢你。

4

1 回答 1

0

尝试使用 Join 嵌套子查询。如下所示:

select a.Node, a.Node_id, b.Zone_ref from (
 select 
 [from_bus] as [Node], 
 [from_bus_id]  as [Node_id]
 from table1 
 union 
 select 
 [to_bus] as [Node], 
 [to_bus_id]  as [Node_id]
 from table1
 union
 select 
 [from_bus] as [Node], 
 [from_bus_id]  as [Node_id]
 from table2
 union 
 select 
 [to_bus] as [Node], 
 [to_bus_id]  as [Node_id]
 from table2 ) a
INNER JOIN table3 b ON a.Node = b.Node_Name
于 2018-11-30T13:50:44.537 回答