mysql添加为成绩表添加名次

对于一种这样的表,为score添加名次

======》》

对于mysql来说

名次用临时变量来保存,考虑对于相同的分数名次一样,可以先将表按分数分组,与名次单独查询出来

select @counter:=@counter+1 as rank,t.score from (select @counter:=0,score from user group by score order by score desc)as t

 

然后将该表与原表关联,为每个人添加名次

合起来的一句sql就是

select u.id,u.username,u.score,a.rank as 名次 from user u,
(select @counter:=@counter+1 as rank,t.score from (select @counter:=0,score from user group by score order by score desc)as t)a
where u.score=a.score;

   

       

        

posted on 2016-10-15 13:24  pokid  阅读(2115)  评论(0编辑  收藏  举报

导航