数据库:
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名球员仅仅平均得分球队得分就是我迷失的地方。
答案 0 :(得分:2)
SELECT charGuild, AVG(charRating) as gr FROM ins_rated GROUP BY charGuild order by gr desc limit 2
答案 1 :(得分:0)
错误代码:1235此版本的MySQL尚不支持'LIMIT& IN / ALL / ANY / SOME子查询'
我认为你可能需要使用一些编码。
我能做的最好的事情是:
SELECT charGuild, charRating
FROM ins_rated a
WHERE charRating = (SELECT MAX(charRating) FROM ins_rated b WHERE b.charGuild = a.charGuild)
OR charRating = (SELECT MAX(charRating) FROM ins_rated b WHERE b.charGuild = a.charGuild
AND charRating < (SELECT MAX(charRating) FROM ins_rated c WHERE c.charGuild = a.charGuild));
输出:
charGuild charRating
team 1 100
team 1 90
team 2 100
team 2 95
编辑:抓一点。它获得了2个最高唯一分数。如果球队中有2名球员获得100分,则不会选择(100,100)作为最高分,但是(100,95)。
答案 2 :(得分:0)
哇噢!
SELECT charGuild, AVG(charRating) average
FROM (SELECT charRating, charGuild FROM ins_rated a ORDER BY charRating DESC LIMIT 4) top4
GROUP BY charGuild
输出:
charGuild average
team 1 95.0000
team 2 97.5000
编辑:抓一点。这个假设团队数量众所周知,每个团队至少有2人。
答案 3 :(得分:0)
我喜欢这个: 它没有我其他2个答案的任何缺陷......
SELECT charGuild, AVG(charRating) average
FROM (
SELECT
charRating,
charGuild,
@num := IF(@guild = charGuild, @num + 1, 1) AS row_number,
@guild := charGuild
FROM ins_rated a
ORDER BY charGuild DESC
) ordered
WHERE row_number <= 2
GROUP BY charGuild;
输出:
charGuild average
team 1 95.0000
team 2 97.5000