场景是我有一个固定装置列表如下所示。固定由团队ID表示
灯具表
FixtureID HomeTeam AwayTeam
1 1 2
2 2 4
然后我需要查看Team表以获取每个团队的名称
团队表
TeamID TeamName
1 Team1
2 Team2
我需要运行一个查询,它将获取灯具并将TeamID更改为团队名称,因此它看起来像
HomeTeam TeamName AwayTeam TeamName
1 Team1 2 Team2
2 Team2 4 Team4
目前我有
SELECT Fixtures.HomeTeam, Teams.TeamName, Fixtures.AwayTeam
FROM Fixtures INNER JOIN
Teams ON Fixtures.HomeTeam = Teams.TeamID
哪个输出:
HomeTeam TeamName AwayTeam
1 Donegal Celtic 2
2 Banbridge Town 1
3 Limavdy United 5
我只是难以让球队的名字显示出来。任何帮助都会受到赞赏。
答案 0 :(得分:2)
您只需要两个连接:
SELECT f.HomeTeam, th.TeamName, f.AwayTeam, ta.TeamName
FROM Fixtures f INNER JOIN
Teams th
ON f.HomeTeam = th.TeamID INNER JOIN
Teams ta
ON f.AwayTeam = ta.TeamId;
答案 1 :(得分:1)
你需要join
两次团队表,例如:
SELECT f.FixtureID, t1.TeamID, t1.name, t2.TeamID, t2.name
FROM fixtures f JOIN team t1 ON f.hometeam = t1.TeamID
JOIN team t2 on f.awayteam = t2.TeamID;
这是 SQL Fiddle 。
答案 2 :(得分:1)
使用Teams
表格加入Fixtures
表格两次。
SELECT Fixtures.HomeTeam, t1.TeamName, Fixtures.AwayTeam, t2.TeamName
FROM Fixtures INNER JOIN
Teams t1
ON Fixtures.HomeTeam = t1.TeamID
INNER JOIN
Teams t2
ON Fixtures.AwayTeam = t2.TeamId;
Working示例。