根据分数,通过sql语句实现排名

可全局排序,也可以根据已有字段(sn)排序:

表(t1)结构:

查询语句:

set @chengji=0;
set @mingci=1;
set @shunxu=0;
/*sql语句*/
select xingming,chengji,mingci
    from
(select xingming,chengji,@shunxu:=@shunxu+1,
if(chengji=@chengji,@mingci,@mingci:=@shunxu) as mingci,
@chengji:=chengji
 from t1 where sn=1 order by chengji desc) as t ;

 更新语句:

set @chengji=0;
set @mingci=1;
set @shunxu=0;
/*sql语句*/
update t1,
(select xingming,chengji,
@shunxu:=@shunxu+1,
if(chengji=@chengji,@mingci,@mingci:=@shunxu) as mingci,
@chengji:=chengji
from t1 where sn=2  order by chengji desc) as t2
set t1.mingci=t2.mingci
WHERE t1.xingming=t2.xingming and t1.chengji=t2.chengji and t1.sn=2;

更新结果:

 

根据原文:http://bbs.csdn.net/topics/250014224 扩展,记录一下!

posted @ 2015-05-15 10:54  问君知否  阅读(523)  评论(0)    收藏  举报