我正在做一个项目,我需要从表格中提取按降序排列的每个团队的前 5 名得分的总和。
这是表结构
谁能帮我这个查询
谢谢
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
SELECT sum(score) as total
FROM
(SELECT score FROM your_table ORDER BY by score DESC LIMIT 1,5)
$count = 0;
foreach($this->conn->query("SELECT * FROM scores DESC LIMIT 5") as $rows){
$count += $rows;
}
换成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;