gpt4 book ai didi

php - 如何在 MySQL 中选择 TOP n 分组值的 AVG?

转载 作者:行者123 更新时间:2023-11-29 02:36:09 26 4
gpt4 key购买 nike

数据库:

player  |  team  |  points  
player1 | team1 | 100
player2 | team1 | 90
player3 | team2 | 100
player4 | team2 | 95
player5 | team2 | 90

我试图从每支球队的积分中获得前 2 名球员,并将他们平均计算到球队排名中,同时在查询中对球队进行分组:

team2 97.5 (not 95)
team1 95


`$mysqli->query("SELECT charGuild, gr FROM (
SELECT charGuild, AVG(charRating) as gr
FROM ins_rated
GROUP BY charGuild
HAVING COUNT(*) >= 10
ORDER BY gr DESC
LIMIT 15
)
ORDER BY gr DESC
LIMIT 40");`

没有按预期工作。

$mysqli->query("SELECT charGuild, AVG(charRating) AS gr 
FROM ins_rated
GROUP BY charGuild
HAVING COUNT(*) >= 10
ORDER BY gr DESC
LIMIT 40");

列出了数据库中至少有 10 人的顶级团队。现在添加一种方法让前 15 名球员只计算平均球队得分是我迷路的地方。

最佳答案

SELECT charGuild, AVG(charRating) as gr FROM ins_rated GROUP BY charGuild order by gr desc limit 2 

关于php - 如何在 MySQL 中选择 TOP n 分组值的 AVG?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4816506/

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