我有一个名为PlayerFriends的表来模拟多对多的关系。
PlayerFriends表有PlayerFriendPlayerID和PlayerFriendFriendID。它们是外键。
Players表中的Player有一个PlayerName字段。
我想为玩家选择所有朋友的所有玩家名称' pid'。
我现在有一个问题可以让我所有的朋友:
SELECT * FROM PlayerFriends WHERE PlayerFriends.PlayerFriendPlayerID = pid;
但我怎样才能选择朋友的玩家名称?
由于
答案 0 :(得分:3)
您可以使用简单的Join
:
Select P.PlayerName
From PlayerFriends F
Join Players P On F.PlayerFriendFriendId = P.PlayerId
Where F.PlayerFriendPlayerId = pid
答案 1 :(得分:2)
尝试:
SELECT C.PlayerName
FROM Players AS A
JOIN PlayerFriends AS B ON B.PlayerId=A.id
JOIN Players AS C ON B.PlayerFriendPlayerID=C.id
WHERE A.id=pid