将两个 table 的查询与另一个 table 合并
Join a query of two tables with another table
我需要帮助才能加入三个 table。我可以以某种方式加入两个 tables table1
和 table2
以获得所需的输出,但我想加入
另一个 table 以获得一些新的相关专栏。
这是我的查询:
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_ID是唯一的。
现在,我有另一个 table3,我需要另一个列 (Zone_ref)
,其中包含相应 Zone
.
的 IDs
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
我可以在一些公共字段中加入 table,但不知道如何将来自两个 table 的一个查询与第三个 table 集成。需要你的建议。顺便说一句,我正在使用访问数据库。谢谢。
尝试使用 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
我需要帮助才能加入三个 table。我可以以某种方式加入两个 tables table1
和 table2
以获得所需的输出,但我想加入
另一个 table 以获得一些新的相关专栏。
这是我的查询:
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_ID是唯一的。
现在,我有另一个 table3,我需要另一个列 (Zone_ref)
,其中包含相应 Zone
.
IDs
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
我可以在一些公共字段中加入 table,但不知道如何将来自两个 table 的一个查询与第三个 table 集成。需要你的建议。顺便说一句,我正在使用访问数据库。谢谢。
尝试使用 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