gpt4 book ai didi

MYSQL:将两个不同的字段连接到第三个表中的同一字段

转载 作者:行者123 更新时间:2023-11-29 07:52:31 25 4
gpt4 key购买 nike

我正在尝试将两个不同的字段连接到第三个查找表中的同一字段。我的数据结构如下:

Investments table:
Investment_ID
Company_Country_Code (e.g., US, UK, AU, FR)
Fund_Country_Code (e.g., US, UK, AU, FR)

Country Lookup Table:
Country_Code (e.g., US, UK, AU, FR)
Country_Name (e.g., United States, United Kingdom, Australia, France)

我想将 Company_Country_Code 和 Fund_Country_Code 加入到国家/地区查找表中的 Country_Code 表中,并提取相应的 Country_Names。我想要这样的数据输出:

(Investment_ID, Company_Country_Name, Fund_Country_Name)
1, United States, France
2, United Kingdom, Australia
3, France, United States

有没有一种特殊的连接来完成这个?

谢谢!

最佳答案

没什么特别的 - 只需进行两个连接:

SELECT 
i.Investment_ID,
c.Country_Name as Company_Country_Name,
f.Country_Name as Fund_Country_Name
FROM
Investments i
JOIN
Country c ON i.Company_Country_Code = c.Country_Code
JOIN
Country f ON i.Fund_Country_Code = f.Country_Code

关于MYSQL:将两个不同的字段连接到第三个表中的同一字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26065840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com