您正在寻找
DENSE_RANK,但它支持高于8.0的mysql版本
看起来像这样。
CREATE TABLE T( User_id int, Score int);insert into t values (1,12258); insert into t values (1,112);insert into t values (2,9678);insert into t values (5,9678);insert into t values (3,689206);insert into t values (3,1868);
查询1 :
SELECt User_id,Score,RankFROM ( SELECt User_id, Score, @rank :=IF(@previous = t1.score, @rank, @rank + 1) Rank, @previous := t1.Score FROM T t1 CROSS JOIN (SELECt @Rank := 0,@previous := 0) r WHERe t1.Score = ( SELECT MAX(Score) FROM T tt WHERe t1.User_id = tt.User_id ) ORDER BY Score desc) t1
结果 :
| User_id | Score | Rank ||---------|--------|------|| 3 | 689206 | 1 || 1 | 12258 | 2 || 2 | 9678 | 3 || 5 | 9678 | 3 |
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)