我有两张像这样的表
name | fNatId | mNatId
=======================
Smith | 1 | 1
Doe | 1 | 0
Owen | 0 | 2
和这一个
id | countryName
================
0 | U.S.
1 | U.K.
2 | Canada
我想把第一张表翻译成这个
Name | Father's Nationality | Mother's Nationality
=====================================================
Smith | U.K. | U.K.
Doe | U.K. | U.S.
Owen | U.S. | Canada
我该怎么做?使用两次LEFT JOIN
投掷Not unique table/alias
错误。只使用一个将两列匹配为父亲或母亲。
答案 0 :(得分:2)
您需要为表使用唯一的别名。将您的第一个表格视为users
,将第二个表格视为countries
,您是否尝试过这种方式?以下应该与您正在寻找的工作完全一致。
SELECT u.name as Name, c1.countryName as FathersNationality, c2.countryName as MothersNationality
FROM users as u
LEFT JOIN countries as c1 ON c1.id = u.fNatId
LEFT JOIN countries as c2 ON c2.id = u.mNatId