鍍金池/ 問答/數(shù)據(jù)庫/ sql 分組排名

sql 分組排名

表結構如下:

    學生id      課程id    分數(shù)
    student_id course_id score
    1          1         60
    1          2         50
    1          3         70
    2          1         20
    2          2         40
    ...
    ...

不考慮總分數(shù)相同的情況下,怎么查詢比student_id=10的總分數(shù)高的人數(shù),即自己的排名情況?

回答
編輯回答
病癮

試試這個看行嗎

# 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); 
2018年7月19日 04:03
編輯回答
孤客
獲取score:'''select score from table where student_id=10'''
獲取分數(shù)比你高的人數(shù):'''select count(1) as cnt from table where score>'{0}' '''.format(score)
2017年9月8日 18:08