mysql 排名

本文详细介绍了如何使用MySQL8和5.7版本实现不同类型的分数排名:不分组排名、连续排名、跳跃并列排名、连续并列排名,以及在分组基础上的连续排名、并列跳跃排名和并列连续排名。通过示例SQL查询,展示了ROW_NUMBER(), RANK()和DENSE_RANK()函数在各种排名场景中的应用。
摘要由CSDN通过智能技术生成

1、不分组排名

1、连续排名
## mysql 8
SELECT
	score,ranking 
FROM
	(
	SELECT
		s.score,
		ROW_NUMBER () OVER ( ORDER BY score DESC ) ranking 
	FROM
		(
		SELECT 0 AS score 
		UNION ALL
		SELECT 0 AS score
	    UNION ALL
		SELECT 0 AS score 
		UNION ALL
		SELECT 1 AS score 
		UNION ALL
		SELECT 2 AS score 
		UNION ALL
		SELECT 1 AS score 
		) s
		ORDER BY
		s.score DESC 
	) a;

## mysql5.7
select score,ranking
from (
	SELECT 
	 s.score,
	(@cur_rank := @cur_rank + 1) AS ranking
	 
	FROM (
		select 0 as score 
		union all
		select 0 as score 
		union all
		select 0 as score 
		union all
		select 1 as score
		union all
		select 2 as score
		union all 
		select 1 as score 
	)s,(SELECT @cur_rank := 0)  r 
	ORDER BY s.score desc
) a 
;

2、跳跃并列排名
## mysql8
SELECT
	score,
	ranking 
FROM
	(
	SELECT
		s.score,
		RANK() OVER(ORDER BY score DESC)as ranking
	FROM
		(
		SELECT
			0 AS score UNION ALL
		SELECT
			0 AS score UNION ALL
		SELECT
			0 AS score UNION ALL
		SELECT
			1 AS score UNION ALL
		SELECT
			2 AS score UNION ALL
		SELECT
			1 AS score 
			) s
	) a;

##mysql 5.7
select score,ranking
from (
	SELECT 
	 s.score,
	@cur_count := @cur_count + 1,
 if(@pre_score = s.score,@cur_rank,@cur_rank := @cur_count) ranking,
 @pre_score := s.score
	FROM (

		select 0 as score 
		union all
		select 0 as score 
		union all
		select 0 as score 
		union all
		select 1 as score
		union all
		select 2 as score
		union all 
		select 1 as score 

	)s,(SELECT @cur_count := 0,@cur_rank:=0,@pre_score := NULL)  r 
	ORDER BY s.score desc
) a 
;

3、连续并列排名
##mysql 8
SELECT
	score,
	ranking 
FROM
	(
	SELECT
		s.score,
DENSE_RANK() OVER(ORDER BY score DESC)as ranking
	FROM
		(
		SELECT
			0 AS score UNION ALL
		SELECT
			0 AS score UNION ALL
		SELECT
			0 AS score UNION ALL
		SELECT
			1 AS score UNION ALL
		SELECT
			2 AS score UNION ALL
		SELECT
			1 AS score 
			) s
	) a;

##mysql5.7
## if  实现连续并列排名
select score,ranking
from (
	SELECT 
	 s.score,
	 IF(@pre_score = score, @cur_rank , @cur_rank :=@cur_rank +1 ) AS ranking,
	@pre_score := score

	FROM (
		select 0 as score 
		union all
		select 0 as score 
		union all
		select 0 as score 
		union all
		select 1 as score
		union all
		select 2 as score
	)s,(SELECT @pre_score := null ,@cur_rank := 0)  r 
	ORDER BY s.score desc
) a 
;

## 连续并列排名  case ...when ... then  遇到零会出现空排名
select score,ranking
from (
	SELECT 
	 s.score,
	 CASE
		WHEN @pre_score = score THEN  @cur_rank
		WHEN @pre_score := score THEN @cur_rank :=@cur_rank +1
	END AS ranking
	 
	FROM (
		select 0 as score 
		union all
		select 0 as score 
		union all
		select 0 as score 
		union all
		select 1 as score
		union all
		select 2 as score
	)s,(SELECT @pre_score := null ,@cur_rank := 0)  r 
	ORDER BY s.score desc
) a 
;

2、分组排名

1、分组连续排名
##mysql 8
select 
cid,score,ranking
from (
	SELECT 
	 s.score,
	 s.cid,
	ROW_NUMBER() OVER (PARTITION BY s.cid ORDER BY s.score DESC) ranking
	 
	FROM (
		select 0 as score , '第一组' as cid
		union all
		select 0 as score , '第一组' as cid
		union all
		select 0 as score  , '第二组' as cid
		union all
		select 1 as score , '第二组' as cid
		union all
		select 2 as score , '第一组' as cid
	)s
) a 
;


##mysql5.7
select 
cid,score,ranking
from (
	SELECT 
	 	s.score,
		s.cid,
	 	IF(@pre_cid = cid, @cur_rank :=@cur_rank +1,@cur_rank :=1) AS ranking,
		@pre_cid := cid
	 
	FROM (
		select 0 as score , '第一组' as cid
		union all
		select 0 as score , '第一组' as cid
		union all
		select 0 as score  , '第二组' as cid
		union all
		select 1 as score , '第二组' as cid
		union all
		select 2 as score , '第一组' as cid
	)s,(SELECT @pre_cid := null ,@cur_rank := 0)  r 
	ORDER BY s.cid,s.score desc
) a 
;

2、分组并列跳跃排名
##mysql 8
select 
cid,score,ranking
from (
	SELECT 
	 	s.score,
		s.cid,
		RANK() OVER(PARTITION BY s.cid ORDER BY s.score DESC) ranking
	 
	FROM (
		select 0 as score , '第一组' as cid
		union all
		select 0 as score , '第一组' as cid
		union all
		select 0 as score  , '第二组' as cid
		union all
		select 1 as score , '第二组' as cid
		union all
		select 2 as score , '第一组' as cid
	)s
) a 
;


##mysql5.7

select 
cid,score,ranking
from (
SELECT s.cid, s.score,
IF(@pre_cid = s.cid,
   @rank_counter := @rank_counter + 1,
   @rank_counter := 1) temp1,
IF(@pre_cid = s.cid,
   IF(@pre_score = s.score, @cur_rank, @cur_rank := @rank_counter),
   @cur_rank := 1) ranking,
@pre_score := s.score temp2,
@pre_cid := s.cid temp3
FROM (	select 0 as score , '第一组' as cid
		union all
		select 0 as score , '第一组' as cid
		union all
		select 0 as score  , '第二组' as cid
		union all
		select 1 as score , '第二组' as cid
		union all
		select 2 as score , '第一组' as cid) s, (SELECT @cur_rank := 0, @pre_cid := NULL, @pre_score := NULL, @rank_counter := 1)r
ORDER BY s.cid, s.score DESC
) a 
;
3、分组并列连续排名
##mysql8
select 
cid,score,ranking
from (
SELECT s.cid, s.score,
DENSE_RANK() OVER(PARTITION BY s.cid ORDER BY s.score DESC)ranking
FROM (	select 0 as score , '第一组' as cid
		union all
		select 0 as score , '第一组' as cid
		union all
		select 0 as score  , '第二组' as cid
		union all
		select 1 as score , '第二组' as cid
		union all
		select 2 as score , '第一组' as cid) s
) a 
;



##mysql.7
select 
cid,score,ranking
from (
SELECT s.cid, s.score,
IF(@pre_score = s.score, @cur_rank, @cur_rank := @cur_rank + 1) temp1,
@pre_score := s.score temp2,
IF(@pre_cid = s.cid, @cur_rank, @cur_rank := 1) ranking,
@pre_cid := s.cid temp3
FROM (	select 0 as score , '第一组' as cid
		union all
		select 0 as score , '第一组' as cid
		union all
		select 0 as score  , '第二组' as cid
		union all
		select 1 as score , '第二组' as cid
		union all
		select 2 as score , '第一组' as cid) s, (SELECT @cur_rank :=0, @pre_score = NULL, @pre_cid := NULL) r
ORDER BY cid, score DESC
) a 
;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值