2014-09-26 69 views
-1

我想加入两个不同的字段到第三个查找表中的同一个字段。我的数据结构是这样的:MYSQL:加入两个不同的字段到第三个表中的相同的字段

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 

是否有一种特殊的连接来完成此?

谢谢!

回答

0

这一切都取决于你想要什么...如果你想筛选出不属于这两个表中,然后使用一个INNER JOIN ..如果你想加入无滤波使用LEFT JOIN

SELECT i.Investment_ID, i.Company_Country_Name, i1.Fund_Country_Name 
FROM country_lookup cl 
JOIN investments i ON i.Company_Country_Code = cl.Country_Code 
JOIN investments i1 ON i1.Fund_Country_Code = cl.Country_Code 
任何结果

没有过滤数据与左连接,你可以做到这一点,像这样

SELECT i.Investment_ID, i.Company_Country_Name, i1.Fund_Country_Name 
FROM country_lookup cl 
LEFT JOIN investments i ON i.Company_Country_Code = cl.Country_Code 
LEFT JOIN investments i1 ON i1.Fund_Country_Code = cl.Country_Code 
0

没有什么特别的 - 只是做两个连接:

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 
相关问题