SQL 按 student_id 对任何课程的唯一标记对行进行分组

SQL group rows by student_id on unique marks for any course

我有一个 SQL table 这样的:

Student_id     mark         course
----------     --------     ---------
1              15           math
1              15           physics
2              15           math
2              16           physics

并希望生成此输出:

Student_id     mark         count
----------     --------     ---------
1              15           2
2              15           1
2              16           1

count 是 每个学生的单个唯一标记数(在任意数量的课程上)

SELECT `student_id`, `mark`, count(1) AS `count`
FROM `the_table`
GROUP BY `student_id`, `mark`
;

如果您希望结果按特定顺序排列,目前 MySql 中的 "GROUP BY" 可以,但我最近听说它在最新版本中已被弃用,因此您可能想要添加在 GROUP BY 之后的 ORDER BY 有点面向未来。