我无法正确显示结果,我想要一份包含最高分的所有学生的列表。我的表格设置为记录每次考试尝试,因此每个学生都有多个分数。
这是我的代码:
SELECT o1.student_id, o1.max_score, o2.course_id max_score
FROM (
SELECT student_id, MAX( test_result ) max_score
FROM test_scores
WHERE test_scores.course_id = $course_id
GROUP BY score DESC
)o1
JOIN test_scores o2 ON ( o2.score = o1.max_score AND o2.student_id = o1.student_id )
GROUP BY o1.max_score DESC
我得到这些结果:
course_id max_score student_id_of_max_score
1 100 23
1 90 17
1 80 16
1 60 11
1 40 18
1 39 17*
1 33 1
1 0 11*
我应该补充一点,问题是存在重复学生 ID 的行 - 用星号标记。我只想要每门类(class)的最高分。
最佳答案
任何原因
SELECT course_id, student_id, MAX(score)
FROM test_scores
GROUP BY course_id, student_id
还不够吗?
关于php - 如何从类(class)最新测试中选择最佳成绩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7997670/