mysql8中实现数据排名,距上一名的分差
【代码】mysql8中实现数据排名,距上一名的分差。
·
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
更多推荐
已为社区贡献3条内容
所有评论(0)