Mysql排序获取排名的实例代码
代码如下所示:
SELECT @i:=@i+1 rowNum, if(@total=t.s_score,@rank,@rank:=@i) rank,@total:=t.s_score, t.* from( select t1.* ,t2.s_score from student t1 LEFT JOIN score t2 on t1.s_id=t2.s_id and t2.c_id="01" ORDER BY t2.s_score desc )t,(select @i:=0,@rank:=0,@total:=null) s ; SELECT @i:=@i+1 rowNum, if(@total=t.s_score,@rank,@rank:=@rank+1) rank,@total:=t.s_score, t.* from( select t1.* ,t2.s_score from student t1 LEFT JOIN score t2 on t1.s_id=t2.s_id and t2.c_id="01" ORDER BY t2.s_score desc )t,(select @i:=0,@rank:=0,@total:=null) s ;
Mysql 获取成绩排序后的名次
其实就是输出mysql的排序后的行号
RT:获取单个用户的成绩在所有用户成绩中的排名
可以分两步:
1、查出所有用户和他们的成绩排名
select id,maxScore,(@rowNum:=@rowNum+1) as rowNo from t_user, (select (@rowNum :=0) ) b order by t_user.maxScore desc
2、查出某个用户在所有用户成绩中的排名
select u.rowNo from ( select id,(@rowNum:=@rowNum+1) as rowNo from t_user, (select (@rowNum :=0) ) b order by t_user.maxScore desc ) u where u.id="2015091810371700001";
总结
相关推荐
苏康申 2020-11-13
vitasfly 2020-11-12
专注前端开发 2020-10-21
oraclemch 2020-11-06
liuyang000 2020-09-25
FellowYourHeart 2020-10-05
赵继业 2020-08-17
whyname 2020-08-16
Seandba 2020-08-16
dbasunny 2020-08-16
拼命工作好好玩 2020-08-15
langyue 2020-08-15
写程序的赵童鞋 2020-08-03
Accpcjg 2020-08-02
tydldd 2020-07-30
好记忆也需烂 2020-07-28
jianghero 2020-07-28