查询可以获取玩家的名字,如果它不是空的

时间:2015-11-05 18:42:02

标签: mysql sql

我不知道该怎么称呼,但基本上,我的数据库有1到4名玩家可以玩的游戏。

Games表有PlayerGames表的4个外键。 GameFirstPlace,GameSecondPlace ......等等。它们可以为空。

如果不是,则他们指向PlayerGames表中的条目。

PlayerGames表除其他外还包含players表的外键。 players表具有PlayerName。

我想为PlayerGame不为空的游戏的所有参与者获取PlayerName。

也就是说,如果我的游戏看起来像:

GameFirstPlace   GameSecondPlace   GameThirdPlace   GameFourthPlace
     6                   7               NULL            NULL

Then PlayerGame id 6 has PlayerID = 7 and PlayerGame id 7 has PlayerID 3
Then Player with id 7 PlayerName = 'Jack' and Player with id 3 PlayerName = 'Mary'

然后我的查询可能会返回:

GameID   FirstPlaceName   SecondPlaceName ThirdPlaceName FourthPlaceName
   5          'Jack'          'Mary'         NULL           NULL

对于像这样的东西的选择查询可能是什么样的?

3 个答案:

答案 0 :(得分:1)

您必须以四种方式复制所有连接逻辑。

select
    g.GameID,
    p1.PlayerName as FirstPlaceName,
    p2.PlayerName as SecondPlaceName,
    p3.PlayerName as ThirdPlaceName,
    p4.PlayerName as FourthPlaceName
from
    Games g
    left outer join PlayerGames pg1 on pg1.PlayerID = g.GameFirstPlace
    left outer join Players p1 on p1.PlayerID = pg1.PlayerID
    left outer join PlayerGames pg2 on pg2.PlayerID = g.GameSecondPlace
    left outer join Players p2 on p2.PlayerID = pg2.PlayerID
    left outer join PlayerGames pg3 on pg3.PlayerID = g.GameThirdPlace
    left outer join Players p3 on p3.PlayerID = pg3.PlayerID
    left outer join PlayerGames pg4 on pg4.PlayerID = g.GameFourthPlace
    left outer join Players p4 on p4.PlayerID = pg4.PlayerID

我猜测您的PlayerGame表格有GameID,您可以利用它来简化连接逻辑。输出变得有点复杂,但查询可能会表现得更好。

SELECT
    g.GameID,
    min(case when p.PlayerID = g.GameFirstPlace  then p.PlayerName end) AS FirstPlaceName,
    min(case when p.PlayerID = g.GameSecondPlace then p.PlayerName end) AS SecondPlaceName,
    min(case when p.PlayerID = g.GameThirdPlace  then p.PlayerName end) AS ThirdPlaceName,
    min(case when p.PlayerID = g.GameFourthPlace then p.PlayerName end) AS FourthPlaceName
FROM
    Games g
    inner join PlayerGames pg on pg.GameID = g.GameID
    inner join Players p on p.PlayerID = pg.PlayerID
GROUP BY
    g.GameID

答案 1 :(得分:1)

SELECT 
    Game.GameID
    ,Player1.Name AS FirstPlaceName
    ,Player2.Name AS SecondPlaceName
    ,Player3.Name AS ThirdPlaceName
    ,Player4.Name AS FourthPlaceName
FROM
    Game
    LEFT OUTER JOIN PlayerGame as PlayerGame1 ON Game.GameFirstPlace = PlayerGame1.Id
    LEFT OUTER JOIN Player AS Player1 ON PlayerGame1.PlayerId = Player1.Id
    LEFT OUTER JOIN PlayerGame as PlayerGame2 ON Game.GameSecondPlace = PlayerGame2.Id
    LEFT OUTER JOIN Player AS Player2 ON PlayerGame2.PlayerId = Player2.Id
    LEFT OUTER JOIN PlayerGame as PlayerGame3 ON Game.GameSecondPlace = PlayerGame3.Id
    LEFT OUTER JOIN Player AS Player3 ON PlayerGame3.PlayerId = Player3.Id
    LEFT OUTER JOIN PlayerGame as PlayerGame4 ON Game.GameFirstPlace = PlayerGame4.Id
    LEFT OUTER JOIN Player AS Player4 ON PlayerGame4.PlayerId = Player4.Id

答案 2 :(得分:0)

  SELECT GameID, 
         P1.PlayerName FirstPlaceName,
         P2.PlayerName SecondPlaceName,
         P3.PlayerName ThirdPlaceName,
         P4.PlayerName FourthPlaceName
  FROM Games G
  LEFT JOIN PlayerGames P1
         ON  G.GameFirstPlace  = P1.PlayerID
  LEFT JOIN PlayerGames P2
         ON  G.GameSecondPlace = P2.PlayerID
  LEFT JOIN PlayerGames P3
         ON  G.GameThirdPlace  = P3.PlayerID
  LEFT JOIN PlayerGames P4
         ON  G.GameFourthPlace = P4.PlayerID