我有两个用于纸牌游戏管理的MYSQL表:
第一个,匹配表:
ID ATeam BTeam Player1 Player2
001 T1 T2 1 2
还有另一张表(玩家表),其中包含玩家的ID和姓名:
ID Name
1 Dave
2 Martin
我要做的是使用一个MYSQL SELECT获取当前指定了player1(例如ID player = 1)的所有匹配项,并返回玩家的名字,而不是ID。 为了实现这一点,我需要查询玩家表,但我不知道该使用什么:INNER JOIN也许??
结果应为:
001 T1 T2 Dave Martin
而不是
001 T1 T2 1 2
有什么想法吗?
谢谢!
答案 0 :(得分:2)
这是一个简单的连接查询:
SELECT m.ID, m.ATeam, m.BTeam, p1.Name, p2.Name
FROM Matches AS m
JOIN Players AS p1 ON m.Player1 = p1.ID
JOIN Players AS p2 ON m.Player2 = p2.ID
WHERE m.Player1 = ?
答案 1 :(得分:1)
select m.id, m.ATeam, m.BTeam, p1.name, p2.name from matches m inner join player p1 inner join player p2 on m.player1 = p1.id and m.player2 = p2.id;
答案 2 :(得分:1)
与@Giorgos Betsos相同,但使用左连接
SELECT Matches.ID, Matches.ATeam, Matches.BTeam, p1.Name, p2.Name
FROM Matches
LEFT JOIN Players AS p1 ON (Matches.Player1 = p1.ID)
LEFT JOIN Players AS p2 ON (Matches.Player2 = p2.ID)