mysql实现row_number()和row_number() over(partition by)

row_number()
select @rownum:=@rownum+1 as rn,t.* from (select @rownum:=0) r, test_table t

解释:给test_table里的数据设置行号, rn是行号

 

row_number() over(partition by)

select if(@uid=t.user_id and @cid=t.city_id,@rank:=@rank+1,@rank:=1) as rank,t.*,@uid:=t.user_id,@cid:=t.city_id 
from (select @uid:=null,@cid:=null,@rank:=0) r, (select user_id,city_id,city_name,id from t_user_city order by user_id,city_id,id) t;

 

 

set @rank:=0;
set @CI:=null;
select SId,CId,score,rank from(
select 
SId, CId,score,
@rank:=if(@CI=CId,@rank+1,1) as rank,
@CI:=CId
from data.sc
order by CId,score desc) as t1

 

 

源文 :https://www.jianshu.com/p/3419550152d5

posted @ 2019-10-11 14:02  BloggerSb  阅读(2085)  评论(0编辑  收藏  举报