1、数据列表排名,距上一名的分差的sql语句:

SELECT
        RANK() OVER (ORDER BY vote_num DESC) AS `rank`,
		id,
		`name`,
		vote_num,
		if(pre_vote_num is null, 0, pre_vote_num-vote_num) dif_vote_num
	FROM
		(select 
		    id,
		   `name`, 
     		vote_num,
     		lag(vote_num, 1) over(ORDER BY vote_num DESC) pre_vote_num from nbh_selection_list 
) t

2、数据列表中某条数据的排名,距上一名的分差的sql语句:

SELECT
    `rank`,
		id,
		`name`,
		vote_num,
		if(pre_vote_num is null, 0, pre_vote_num-vote_num) dif_vote_num
	FROM
		(select 
		    RANK() OVER (ORDER BY vote_num DESC) AS `rank`,
		    id,
		   `name`, 
     		vote_num,
     		lag(vote_num, 1) over(ORDER BY vote_num DESC) pre_vote_num from nbh_selection_list 
) t WHERE t.id = 2
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐