8

我有一个看起来像这样的布局:

Player:
PlayerID - PlayerName

Exp
ExpID - PlayerID - ExpChange

我要做的是获取每个玩家名称的 ExpChange 总和,并按 expchange 的总和对其进行排序。(以 SUM(ExpChange) 为分数的高分列表。

这是我到目前为止所拥有的:

SELECT Player.PlayerName, Exp.ExpChange
FROM Player
INNER JOIN Exp
ON Player.PlayerID=Exp.PlayerID
ORDER BY Exp.ExpChange;

但是现在我需要以某种方式通过该玩家的兑换总和对每个玩家进行排序。这可能吗?我试着搜索了一吨,但找不到任何东西。谢谢

PS我也尝试过这样的事情:

SELECT Player.PlayerName, SUM(Exp.ExpChange) AS exp
FROM Player
INNER JOIN Exp
ON Player.PlayerID=Exp.PlayerID
ORDER BY exp;

但它只输出每个人的总 ExpChange,而不是每个单独的玩家。

4

2 回答 2

11

您需要添加一个GROUP BY子句来获取sum()for each player

SELECT Player.PlayerName, SUM(COALESCE(Exp.ExpChange, 0)) AS exp
FROM Player
LEFT JOIN Exp
   ON Player.PlayerID=Exp.PlayerID
GROUP BY Player.PlayerName
ORDER BY exp;

如果您只想要在exp表中匹配的记录,那么您可以使用INNER JOIN

SELECT Player.PlayerName, SUM(Exp.ExpChange) AS exp
FROM Player
INNER JOIN Exp
   ON Player.PlayerID=Exp.PlayerID
GROUP BY Player.PlayerName
ORDER BY exp;
于 2013-01-04T20:06:09.917 回答
0

尝试这个

  SELECT P.PlayerName, SUM(Exp.ExpChange) AS exp
  FROM Player p
  INNER JOIN Exp
  ON p.PlayerID=Exp.PlayerID
  GROUP BY p.PlayerName
  ORDER BY exp;
于 2013-01-04T20:08:11.403 回答