根據分數,經過sql語句實現排名

可全局排序,也能夠根據已有字段(sn)排序:sql

表(t1)結構:spa

查詢語句:.net

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 ;

 更新語句:code

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;

更新結果:blog

 

根據原文:http://bbs.csdn.net/topics/250014224 擴展,記錄一下!排序

相關文章
相關標籤/搜索