Table 自行加入
Table Self join
Table_a 有列: old_id 和 new_id 。以下查询给出了 old_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.organization_code = b.organization_code
INNER JOIN table_d d ON d.planning_location_id = b.organization_code
我的要求也是获得 new_id 的组织代码。所以我的输出将是这样的
old_id、planning_region(属于old_id)、organization_code(属于old_id)和organization_code(属于new_id).
Self Join 应该可以,但在这种情况下,我是否需要对所有 4 个表进行 self join?
注意:new_id 也可以像 old_id 一样与 table_b 连接。
如果我没理解错的话,你可以添加更多的连接。
如果你只想要新的 organization_code
:
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_region
,那我们还要带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
旁注:table c
在查询中的目的并不明显(除此之外,也许是过滤?)。
Table_a 有列: old_id 和 new_id 。以下查询给出了 old_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.organization_code = b.organization_code
INNER JOIN table_d d ON d.planning_location_id = b.organization_code
我的要求也是获得 new_id 的组织代码。所以我的输出将是这样的
old_id、planning_region(属于old_id)、organization_code(属于old_id)和organization_code(属于new_id).
Self Join 应该可以,但在这种情况下,我是否需要对所有 4 个表进行 self join?
注意:new_id 也可以像 old_id 一样与 table_b 连接。
如果我没理解错的话,你可以添加更多的连接。
如果你只想要新的 organization_code
:
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_region
,那我们还要带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
旁注:table c
在查询中的目的并不明显(除此之外,也许是过滤?)。