2014-01-18 112 views
-1

我需要你的帮助。想我也下假表 enter image description here从两个不同的表中选择

,另一个假表 enter image description here

我想什么是拿出了下表

enter image description here

select b.Country, b.AnotherCode, a.Code from Country1 a, Country2 b where a.Country=b.Country 

(主关键是国家)但它不显示最后两行。如何做,以便它将检索最后一个表中显示的所有行。由于inadvance

回答

2
select b.Country, b.AnotherCode, a.Code from Country1 a left join Country2 b on a.Country=b.Country 

使用左连接

0

你返回两个集合的交集 - 如果你想要的所有行任一组,你可以使用完整的联接:

SELECT  COALESCE(a.Country, b.Country) AS Country, 
      COALESCE(b.AnotherCode, 0) AS AnotherCode, 
      COALESCE(a.Code, 0) AS Code 
FROM  Country1 a 
FULL JOIN Country2 b 
    ON  a.Country = b.Country 

当然,如果您只需要Country1的记录和Country2的任何对应值,那么LEFT JOIN就足够了。同样,如果您在加入失败时想要一个不同的默认值,请更改COALESCE