我正在开展一个项目,我需要从表格中按降序排序每个团队的前5名总分。
这是表结构
任何人都可以帮我解决这个问题
由于
答案 0 :(得分:4)
SELECT team_id AS `team` , (
SELECT SUM( score )
FROM `table`
WHERE team_id = `team`
ORDER BY score DESC
LIMIT 5
) AS `score`
FROM `table`
GROUP BY team_id
ORDER BY `score` DESC
答案 1 :(得分:2)
SELECT sum(score) as total
FROM
(SELECT score FROM your_table ORDER BY by score DESC LIMIT 1,5)
答案 2 :(得分:1)
$count = 0;
foreach($this->conn->query("SELECT * FROM scores DESC LIMIT 5") as $rows){
$count += $rows;
}
答案 3 :(得分:0)
将foo
替换为您的姓名。
SELECT team_id, SUM(score) AS `score`
FROM `foo`
WHERE
(SELECT COUNT(*)
FROM `foo` AS t1
WHERE t1.score >= `foo`.score) <= 5
GROUP BY team_id
ORDER BY `score` DESC;