mysql - SUM 后获取 SQL 中的最大值

标签 mysql

我有一个 SQL 表格,其中包含一场比赛中 1 个 Student_id 的 2 分,如下图 enter image description here 所示

现在像这样有很多Student_profile_IDBasketball_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 bylimit。想法如下:

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/

相关文章:

mysql - 这个存储过程有什么问题?

mysql - 一对多 MongoDB 与 SQL

php - 打印 mysqli 查询结果

php - mySQL 语法有什么问题?

java - 如何在 Hibernate 中插入外键定义为 Long 的实体?

PHP:即使有多个具有相同名称的复选框,也将提交的数据保留在表单中

php - 如何从 sql 数据库的一列填充 php 数组?

php - 使用 Dompdf 的 While 循环输出表

MySQL:计算许多连接表中的项目

Mysql 正则表达式返回零结果