我在下面定义了两个表团队和游戏:
Teams
teamID Name rank
-----------------
1 UNC 8
2 Duke 9
3 Cal 5
4 UNLV 12
Games
gameID team1ID team2ID score1 score2
------------------------------------
1 1 2 88 87
2 3 4 86 85
我想要一个返回的查询:
gameID team1name team2name score1 score2
------------------------------------------------
1 UNC Duke 88 87
2 Cal UNLV 86 85
我已经可以使用以下语句的结果了,但是,我知道必须有一个正确的方法。
SELECT * FROM games INNER JOIN
teams ON teams.teamID = games.team1ID
OR teams.teamID = games.team2ID
感谢您的任何帮助。
最佳答案
用不同的别名将teams
表连接两次
SELECT g.gameID,
t1.name as team1,
t2.name as team2,
g.score1, g.score2
FROM games g
INNER JOIN teams t1 ON t1.teamID = g.team1ID
INNER JOIN teams t2 ON t2.teamID = g.team2ID
关于mysql - 在mysql中返回一个“名称”列代替一个ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36484581/