gpt4 book ai didi

mysql - SQL获取每个PlayerID的行总和

转载 作者:可可西里 更新时间:2023-11-01 06:32:38 25 4
gpt4 key购买 nike

我的布局看起来像这样:

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;

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

附言我也尝试过这样的事情:

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

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

最佳答案

您需要添加一个 GROUP BY 子句来获取每个 playersum():

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;

关于mysql - SQL获取每个PlayerID的行总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14164103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com