如何从mysql中的表中检索5个最高分的总和

时间:2013-09-27 15:57:26

标签: php html mysql database

我正在开展一个项目,我需要从表格中按降序排序每个团队的前5名总分。

这是表结构

enter image description here

任何人都可以帮我解决这个问题

由于

4 个答案:

答案 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;