我有一个 SQL 表格,其中包含一场比赛中 1 个 Student_id 的 2 分,如下图 所示
现在像这样有很多Student_profile_ID和Basketball_Match_ID。我想让 Student_profile_ID 相同,然后在“想要获取其中得分最高的人”后面添加分数。
我能够求和,但无法获得最大值
SELECT student_profile_id,
sum(point) as total_point
from `basketball_points`
where student_profile_id = {$value2}
AND basketball_match_id = {$key3}
Foreach 循环帮助我增加 PHP 中的值。并检查所有结果
最佳答案
我认为您需要group by
以及order by
和limit
。想法如下:
select student_profile_id, sum(point) as total_point
from `basketball_points`
where basketball_match_id = {$key3}
group by student_profile_id
order by total_point desc
limit 1;
注意:如果多名学生都达到最大值,则只会为您提供一名学生。另外,还不清楚您是否需要 where
子句。
如果您想要所有学生的总分,请省略 limit
子句。
关于mysql - SUM 后获取 SQL 中的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34975970/