首页 > 技术文章 > MySQL 的 RowNum 实现(排行榜计算用户排名)

phpfans 2015-10-17 20:46 原文

1. 计算用户排名最高效的方法
例如:通过用户分享个数排名,那么自己的排名就是:比自己分享数多的用户个数 + 1
select count(*) + 1 as count from top where `status`= 0 and `day` = '144022222' and `count` > '自己分享个数'

缺点:当多个用户分享个数相同的时候,会出现排名相同的情况

 

2. MySQL 下面没有RowNum,排序后序号却无法得到,比较麻烦!

在网上找了再三,通过比较,确认了以下的方法是可行的 :

SELECT @rownum:=@rownum+1 rownum, CollectSn From
(SELECT @rownum:=0,bbgmain.* FROM qbdb.bbgmain WHERE collectsn!='' ORDER BY collectsn limit 10) t

按照点击数排序,查询new表里的文章排名

select * from (
    select @rank:=@rank+1 as rank, id, title from (select @rank:=0, news.* from news order by dian_num desc) rank) 
news_rank where id = 4;

推荐阅读