欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Mysql排序获取排名的实例代码

程序员文章站 2022-03-27 08:15:38
代码如下所示: select @i:=@i+1 rownum, if(@total=t.s_score,@rank,@rank:=@i) rank,@total...

代码如下所示:

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"; 

总结

以上所述是小编给大家介绍的mysql排序获取排名,希望对大家有所帮助