使用Inner Join&在一个查询中不同

时间:2014-04-16 08:45:46

标签: sql sql-server

我有两个表格(如下所示),名为Merge_Codes&因素。我正在尝试编写一个查询,该查询从因子表中获取不同的国家/地区,并将其连接到Merge_Codes表并返回该区域。

请查看底部的结果表,看看我的意思。我试图在一个查询中执行此操作但是在解决方案附近没有任何地方,正如您可以从下面的sql中看到的那样。

SELECT * FROM Merge_Codes
INNER JOIN ON
(SELECT DISTINCT Country FROM Factors)

Merge_Codes表

Region     Country 
EU         Germany
EU         France
EU         Italy
Asia       Japan
Asia       Hong Kong
NA         Canada
NA         USA
SA         Brazil
SA         Peru
SA         Chile

因素表

Factor     Country 
ABC        Germany      
ABC        Germany      
ABC        Japan        
ABC        USA          
ABC        USA          
ABC        Hong Kong    

结果

Country    Region
Germany    EU
Japan      Asia
USA        NA
Hong Kong  Asia

2 个答案:

答案 0 :(得分:3)

select mc.*
from Merge_Codes mc
join (select distinct Country FROM Factors) f
  on mc.Country = f.Country

答案 1 :(得分:2)

select
    MC.Country,
    MC.Region
from 
    (SELECT DISTINCT Country FROM Factors) DC
    INNER JOIN Merge_Codes MC ON MC.Country = DC.Country