0

我正在做一个项目,我需要为从表中按降序排序的每个团队提取前 5 个 score_rank 的总和。这是表结构

在此处输入图像描述

这是结构的链接

http://kolkata-web-design.co.in/test/structure.htm

这是我正在尝试的查询

SELECT team_id AS `team` ,  (SELECT SUM(score_rank)
FROM `contest_result_total`
WHERE team = `team_id`
ORDER BY score_rank DESC
LIMIT 5
) AS `score`
FROM `contest_result_total`
GROUP BY team_id
ORDER BY `score` DESC

但它不会给出前 5 名的分数,而是计算所有比赛的总和,而不仅仅是按团队 ID 分组的 5

谁能帮我。谢谢

4

2 回答 2

1
SELECT team_id,sum(score_rank)
FROM contest_result_total
WHERE (SELECT COUNT(*)
      FROM contest_result_total AS c
      WHERE c.team_id = contest_result_total.team_id
        AND contest_result_total.score_rank <= c.score_rank) <= 5
GROUP BY team_id
于 2013-10-05T12:36:08.627 回答
0

尝试这个:

SELECT team_id AS `team` ,  (SELECT SUM(score_rank)
FROM `contest_result_total`
WHERE team = `team_id`
GROUP BY team_id
ORDER BY score_rank DESC
LIMIT 5
) AS `score`
FROM `contest_result_total`
ORDER BY `score` DESC
于 2013-10-05T07:35:50.773 回答