在下表中,无论类(class)如何,我都需要为学生选择最高分。可能的分数是 5、4 到 0。我可以使用基本的 select distinct score order by
来获得结果。但是该语句扫描全表并进行排序,比较耗时。由于目标只有 5 个可能的值,我相信存在效率更高的解决方案。
CREATE TABLE `course_score` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`create_time` datetime DEFAULT NULL,
`score` int(5) DEFAULT NULL,
`course_id` int(11) DEFAULT NULL,
`student_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=100048 DEFAULT CHARSET=utf8;
最佳答案
可以使用 max() 聚合函数
select max(score)
from course_score
或者如果您需要学生明智的最高分数,您可以执行以下操作 -
select student_id,max(score)
from course_score group by student_id
关于mysql - 从 mysql 表中选择列中的最大数时效率更高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54284057/