Mysql从同一个父表中连接多个列

时间:2012-07-09 13:57:33

标签: mysql join

我遇到了mysql查询的问题。

给出2个表


Id |名称

1 | John Doe

2 |简·多伊

3 |冥王星


门票

Id |所有者|开瓶器| AssignedTo

1 | 1 | 2 | 3

2 | 3 | 1 | 2

所有者,开启者,AssignedTo是链接到人员ID的外键


我需要创建一个查询,用用户名替换Owner,Opener,AssignedTo列。

只有一列(使用左连接就足够了)不是问题,但是我无法接收以下输出

select * from ....

1 | John Doe Jane Doe |冥王星

2 |冥王星| John Doe简·多伊

提前致谢。

2 个答案:

答案 0 :(得分:2)

您可以多次加入Person

SELECT t1.Id, t2.Name AS owner, t3.Name AS opener, t4.Name AS assignedTo
FROM Tickets t1
LEFT JOIN Person t2 ON t1.Owner = t2.Id
LEFT JOIN Person t3 ON t1.Opener = t3.Id
LEFT JOIN Person t4 ON t1.AssignedTo= t4.Id

答案 1 :(得分:0)

SELECT Tickets。Id,POwner.Name AS Owner,POpener.Name AS Opener,PAssignedTo.Name AS AssignedTo 来自Tickets 加入Person AS POwner ON Owner = POwner.Id 加入Person AS POpener ON Opener = POpener.Id 加入Person AS PAssignedTo ON AssignedTo = PAssignedTo.Id