[英]MySQL Inner Join Between Two Tables
I'm trying to JOIN two tables in MySQL
. 我正在尝试在
MySQL
联接两个表。 Here is my table: 这是我的桌子:
Games Table : 游戏表 :
GameID Date/Time PlayerOneID PlayerTwoID
13 12/10/2013 10:53:29 PM 1 2
14 12/10/2013 10:57:29 PM 1 2
15 12/10/2013 10:58:29 PM 2 1
I have another table contain the ID of a player and that players name. 我有另一个表,其中包含一个玩家的ID和该玩家的名字。
Players Table : 玩家表 :
1 Dan
2 Jon
I'd like the resulting table to look like the following: 我希望结果表如下所示:
GameID Date/Time PlayerOneID PlayerTwoID
13 12/10/2013 10:53:29 PM Dan Jon
14 12/10/2013 10:57:29 PM Dan Jon
15 12/10/2013 10:58:29 PM Jon Dan
Here's what I am currently doing: 这是我目前正在做的事情:
SELECT Games.GameID, Games.`Date/Time`, Players.Name, PlayerTwoID
FROM Games
INNER JOIN Players
ON PlayerOneID = Players.ID
This gets PlayerOnes name, but I can't find away to get PlayerTwos name as well. 这获得了PlayerOnes的名称,但是我也找不到获得PlayerTwos的名称的方法。 I have tried using two INNER JOINS, but this hasn't worked.
我尝试使用两个INNER JOINS,但这没有用。 I've read a lot of posts here on the stack, but I haven't come across any solution that works.
我已经在堆栈上阅读了很多文章,但是没有遇到任何可行的解决方案。 I am new to MySQL, so a follow up explanation would be very helpful.
我是MySQL的新手,所以后续解释将非常有帮助。
You were on the right track, you do need to join to the Players table twice, like so: 您处在正确的轨道上,您确实需要两次加入Players表,如下所示:
SELECT Games.GameID, Games.`Date/Time`, p1.Name, p2.Name
FROM Games
INNER JOIN Players p1
ON PlayerOneID = p1.ID
INNER JOIN Players p2
ON PlayerTwoID = p2.ID
What you probably missed was using an alias (p1,p2) to differentiate between the two copies of the Players table you are joining to. 您可能错过的是使用别名(p1,p2)来区分要加入的Players表的两个副本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.