Mysql 从两个不同的表中选择

Mysql 从两个不同的表中选择,mysql,sql,left-join,Mysql,Sql,Left Join,我需要你的帮助。假设我有下面的假表 还有一张假桌子 我想做的是得出下表 select b.Country, b.AnotherCode, a.Code from Country1 a, Country2 b where a.Country=b.Country 主键是country,但不显示最后两行。如何这样做,它将检索上一个表中显示的所有行。谢谢你 select b.Country, b.AnotherCode, a.Code from Country1 a left join Count

我需要你的帮助。假设我有下面的假表

还有一张假桌子

我想做的是得出下表

select b.Country, b.AnotherCode, a.Code from Country1 a, Country2 b where a.Country=b.Country
主键是country,但不显示最后两行。如何这样做,它将检索上一个表中显示的所有行。谢谢你

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

使用左联接

返回两个集合的交集-如果要从任一集合中选择所有行,可以使用完全联接:

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的任何对应值,那么左连接就足够了。类似地,如果希望在连接失败时使用不同的默认值,请更改COALESCE中的值