1

我正在做一个项目,我需要从表格中提取按降序排列的每个团队的前 5 名得分的总和。

这是表结构

在此处输入图像描述

谁能帮我这个查询

谢谢

4

4 回答 4

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
于 2013-09-27T16:09:11.257 回答
2
SELECT sum(score) as total
FROM 
   (SELECT score FROM your_table ORDER BY by score DESC LIMIT 1,5)
于 2013-09-27T16:07:54.697 回答
1
$count = 0;
foreach($this->conn->query("SELECT * FROM scores DESC LIMIT 5") as $rows){
   $count += $rows; 
}
于 2013-09-27T16:01:57.960 回答
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;
于 2013-09-27T16:33:13.860 回答