sql 分组排名

表结构如下:

    学生id      课程id    分数
    student_id course_id score
    1          1         60
    1          2         50
    1          3         70
    2          1         20
    2          2         40
    ...
    ...

不考虑总分数相同的情况下,怎么查询比student_id=10的总分数高的人数,即自己的排名情况?

阅读 2.1k
2 个回答

试试这个看行吗

# table_name为你的表名
SELECT COUNT(student_id) FROM table_name GROUP BY student_id HAVING SUM(score) > (SELECT SUM(score) FROM table_name WHERE student_id = 10); 
获取score:'''select score from table where student_id=10'''
获取分数比你高的人数:'''select count(1) as cnt from table where score>'{0}' '''.format(score)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题