首页 > 解决方案 > 使用sql替换具有不同列名的2列之间的列

问题描述

如何将 home_team_api_id 和 away_team_id 的内容替换为实名 我有 2 个使用 sql 的表。第一个是主客场 ID,第二个是表 1 中对主客场 ID 的描述

表格1

home_team_api_id    away_team_api_id
================    ================
8600                8540
9882                8636
8524                8551
8529                8543
8530                10233

表 2

team_api_id       team_long_name     team_short_name
===========       ==============     ===============
8600                Udinese          UDI
9882                Sampdoria        SAM
8524                Atalanta         ATA
8529                Cagliari         CAG
8530                Catania          CAT
8540                Palermo          PAL
8636                Inter            INT
8551                Siena            SIE
8543                Lazio            LAZ
10233               Genoa            GEN

期望的结果

home_team_api_id    away_team_api_id
================    ================
Udinese              Palermo          
Sampdoria            Inter            
Atalanta             Siena            
Cagliari             Lazio            
Catania              Genoa            

标签: sqljoinmerge

解决方案


table2使用不同的别名加入两次

select home.team_long_name as home_team_api_id,
       away.team_long_name as away_team_api_id
from table1
join table2 as home on home.team_api_id = table1.home_team_api_id
join table2 as away on away.team_api_id = table1.away_team_api_id

推荐阅读