我的数据库中有一个名为characters的表。它跟踪角色ID,名称,marriedto和fathersID。 “marriedto”是他们结婚的角色的角色ID,“fathersID”是该角色父亲的角色ID。
我想在SQL中使用连接语句来提取所有三个名称。 所以我的表可能看起来像这样:
CharID CharName marriedto fatherID
1 Jeremy 2 3
2 Sarah 1 4
3 Charles 5 6
我想运行查询以返回这些行
Jeremy, Sarah, Charles
如果我想让它只给我两个名字,我可以得到一个连接就可以发生,但是有三个是逃避我的。
这适用于两个..
"SELECT C1.name AS spouse1, C2.name AS spouse2
FROM characters AS C1
JOIN characters AS C2
ON C1.marriedto= C2.characters_ID";
我尝试了三次,但它不起作用。
"SELECT C1.name AS spouse1, C2.name AS spouse2 C3.name AS father
FROM characters AS C1
JOIN characters AS C2
ON C1.marriedto= C2.characters_ID
LEFT JOIN characters AS C3
ON C1.fathersID = C3.characters_ID"
如何才能对1个表进行三重连接?
答案 0 :(得分:0)
您的查询看起来很好..我在spouse2
和C3.name
之间只能看到逗号丢失。