Sql 表自联接
表a有两列:旧id和新id。下面的查询给出了旧id的计划区域和组织代码Sql 表自联接,sql,oracle,join,inner-join,Sql,Oracle,Join,Inner Join,表a有两列:旧id和新id。下面的查询给出了旧id的计划区域和组织代码 SELECT a.old_id,d.planning_region,b.organization_code FROM table_a a INNER JOIN table_b b ON a.old_id = b.organization_id INNER JOIN table_c c ON c.organ
SELECT a.old_id,d.planning_region,b.organization_code
FROM table_a a
INNER JOIN table_b b ON a.old_id = b.organization_id
INNER JOIN table_c c ON c.organization_code = b.organization_code
INNER JOIN table_d d ON d.planning_location_id = b.organization_code
我的要求是获取新id的组织代码。所以我的输出是这样的
旧标识、规划区域(旧标识)、组织机构代码(旧标识)和组织机构代码(新标识)
自连接应该可以工作,但在这种情况下,我需要对所有4个表进行自连接吗
注意:新的\u id也可以与旧的\u id一样与表\u b连接。如果我理解正确,您可以添加更多连接 如果您只需要新的
组织机构\u代码
:
SELECT
a.old_id,
d.planning_region,
b.organization_code,
b1.organization_code organization_code_new
FROM table_a a
INNER JOIN table_b b ON a.old_id = b.organization_id
INNER JOIN table_c c ON c.organization_code = b.organization_code
INNER JOIN table_d d ON d.planning_location_id = b.organization_code
INNER JOIN table_b b1 ON a.new_id = b1.organization_id
如果您还需要planning\u地区
,那么我们还需要带d
:
SELECT
a.old_id,
d.planning_region,
b.organization_code,
d1.planning_region planning_region_new,
b1.organization_code organization_code_new
FROM table_a a
INNER JOIN table_b b ON a.old_id = b.organization_id
INNER JOIN table_c c ON c.organization_code = b.organization_code
INNER JOIN table_d d ON d.planning_location_id = b.organization_code
INNER JOIN table_b b1 ON a.new_id = b1.organization_id
INNER JOIN table_c c1 ON a.new_id = c1.organization_id
INNER JOIN table_d d1 ON d1.planning_location_id = b1.organization_code
旁注:表
c
在查询中的用途并不明显(除了,可能是过滤?)如果我理解正确,您可以添加更多联接
如果您只需要新的组织机构\u代码
:
SELECT
a.old_id,
d.planning_region,
b.organization_code,
b1.organization_code organization_code_new
FROM table_a a
INNER JOIN table_b b ON a.old_id = b.organization_id
INNER JOIN table_c c ON c.organization_code = b.organization_code
INNER JOIN table_d d ON d.planning_location_id = b.organization_code
INNER JOIN table_b b1 ON a.new_id = b1.organization_id
如果您还需要planning\u地区
,那么我们还需要带d
:
SELECT
a.old_id,
d.planning_region,
b.organization_code,
d1.planning_region planning_region_new,
b1.organization_code organization_code_new
FROM table_a a
INNER JOIN table_b b ON a.old_id = b.organization_id
INNER JOIN table_c c ON c.organization_code = b.organization_code
INNER JOIN table_d d ON d.planning_location_id = b.organization_code
INNER JOIN table_b b1 ON a.new_id = b1.organization_id
INNER JOIN table_c c1 ON a.new_id = c1.organization_id
INNER JOIN table_d d1 ON d1.planning_location_id = b1.organization_code
旁注:表
c
在查询中的用途并不明显(除了,可能是过滤?)请提供示例数据和所需结果。请提供示例数据和所需结果。c只是将组织代码显示在特定表中。@弧:好的,这就是过滤。这是有道理的。谢谢@GMB!您可能需要将a.new_id=c1.organization_id(最后一行)上的内部联接表_b c1更新为a.new_id=c1.organization_id上的内部联接表_c c1。除了我在new_id列上创建的索引之外,它还起作用了!谢谢:)C只是将组织代码存在于特定的表中。@arc:好的,这就是筛选。这是有道理的。谢谢@GMB!您可能需要将a.new_id=c1.organization_id(最后一行)上的内部联接表_b c1更新为a.new_id=c1.organization_id上的内部联接表_c c1。除了我在new_id列上创建的索引之外,它还起作用了!谢谢:)