MySQL 的 RowNum 實現(排行榜計算用戶排名)

1. 計算用戶排名最高效的方法
例如:經過用戶分享個數排名,那麼本身的排名就是:比本身分享數多的用戶個數 + 1
select count(*) + 1 as count from top where `status`= 0 and `day` = '144022222' and `count` > '本身分享個數'

缺點:當多個用戶分享個數相同的時候,會出現排名相同的狀況post

 

2. MySQL 下面沒有RowNum,排序後序號卻沒法獲得,比較麻煩!

在網上找了再三,經過比較,確認瞭如下的方法是可行的 :spa

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

按照點擊數排序,查詢new表裏的文章排名code

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;
相關文章
相關標籤/搜索