我想加入在另一个表中共享同一个外键的2列
这里是表格:
country:
idcountry| countryname
1 german
2 america
目的地
id|fromcountry |tocountry
1 1 2
我想要的结果:
id|fromc |toc
1 german america
答案 0 :(得分:1)
使用简单的Left Join ans为国家/地区名称列提供别名:
SELECT d.idcountry as id, cf.CountryName as fromc, ct.CountryName as toc
FROM destination d
LEFT JOIN country cf ON d.fromcountry = cf.idcountry
LEFT JOIN country ct ON d.tocountry = ct.idcountry
答案 1 :(得分:0)
对两个字段使用左外连接 像这样的东西
SELECT Dest.ID, CFrom.CountryName, CTo.CountryName
FROM Destination Dest
LEFT OUTER JOIN Country CFrom ON Dest.FromCountry = CFrom.idcountry
LEFT OUTER JOIN Country CTo ON Dest.ToCountry = CTo.idcountry