当前位置:网站首页>MySQL uses and implements ranking functions rank and deny_ Rank and row_ NUMBER
MySQL uses and implements ranking functions rank and deny_ Rank and row_ NUMBER
2022-07-27 01:01:00 【hayhead】
List of articles
stay MYSQL Latest version MYSQL8 The ranking function has been supported
RANK,DENSE_RANKandROW_NUMBER. However, these functions are not supported in the ready version , It can only be achieved by itself . The implementation method mainly uses conditional judgment statements (CASE WHENorIF) And adding temporary variables .
1. Ranking classification
1.1 difference RANK,DENSE_RANK and ROW_NUMBER
- RANK Juxtaposed jump rankings , Juxtaposition is the same value , The same value keeps the duplicate ranking , When you encounter the next different value , Jump to the total ranking .
- DENSE_RANK Side by side continuous sorting , Juxtaposition is the same value , The same value keeps the duplicate ranking , When you encounter the next different value , Still ranked by consecutive numbers .
- ROW_NUMBER Continuous ranking , Even if the same value , Still ranked by consecutive numbers .
The difference is shown in the picture :
1.2 Group ranking
Rank the data after grouping , The difference is shown in the picture :
2. Prepare the data
Create a score table , There are fields in it : fraction score, Course no. course_id And student number student_id.
The implementation is as follows SQL sentence , Import data .
create table score(
student_id varchar(10),
course_id varchar(10),
score decimal(18,1)
);
insert into score values('01' , '01' , 80);
insert into score values('01' , '02' , 90);
insert into score values('01' , '03' , 99);
insert into score values('02' , '01' , 70);
insert into score values('02' , '02' , 60);
insert into score values('02' , '03' , 80);
insert into score values('03' , '01' , 80);
insert into score values('03' , '02' , 80);
insert into score values('03' , '03' , 80);
insert into score values('04' , '01' , 50);
insert into score values('04' , '02' , 30);
insert into score values('04' , '03' , 20);
insert into score values('05' , '01' , 76);
insert into score values('05' , '02' , 87);
insert into score values('06' , '01' , 31);
insert into score values('06' , '03' , 34);
insert into score values('07' , '02' , 89);
insert into score values('07' , '03' , 98);
insert into score values('08' , '02' , 89);
insert into score values('09' , '02' , 89);
View the data :
3. Ranking without grouping
3.1 Continuous ranking
- Use
ROW_NUMBERRealization :
SELECT score,
ROW_NUMBER() OVER (ORDER BY score DESC) ranking
FROM score;
- Use
VariableRealization :
SELECT s.score, (@cur_rank := @cur_rank + 1) ranking
FROM score s, (SELECT @cur_rank := 0) r
ORDER BY score DESC;
The result is shown in Fig. :
3.2 Juxtaposed jump rankings
- Use
RANKRealization :
SELECT course_id, score,
RANK() OVER(ORDER BY score DESC)
FROM score;
- Use
VariableandIFStatements for :
SELECT s.score,
@rank_counter := @rank_counter + 1,
IF(@pre_score = s.score, @cur_rank, @cur_rank := @rank_counter) ranking,
@pre_score := s.score
FROM score s, (SELECT @cur_rank :=0, @pre_score := NULL, @rank_counter := 0) r
ORDER BY s.score DESC;
- Use
VariableandCASEStatements for :
SELECT s.score,
@rank_counter := @rank_counter + 1,
(
CASE
WHEN @pre_score = s.score THEN @cur_rank
WHEN @pre_score := s.score THEN @cur_rank := @rank_counter
END
) ranking
FROM score s, (SELECT @cur_rank :=0, @pre_score := NULL, @rank_counter := 0) r
ORDER BY s.score DESC;
The result is shown in Fig. :
3.3 Juxtaposed in a row
- Use
DENSE_RANKRealization :
SELECT course_id, score,
DENSE_RANK() OVER(ORDER BY score DESC) FROM score;
- Use
VariableandIFStatements for :
SELECT s.score,
IF(@pre_score = s.score, @cur_rank, @cur_rank := @cur_rank + 1) ranking,
@pre_score := s.score
FROM score s, (SELECT @cur_rank :=0, @pre_score = NULL) r
ORDER BY s.score DESC;
- Use
VariableandCASEStatements for :
SELECT s.score,
(
CASE
WHEN @pre_score = s.score THEN @cur_rank
WHEN @pre_score := s.score THEN @cur_rank := @cur_rank + 1
END
) ranking
FROM score s, (SELECT @cur_rank :=0, @pre_score = NULL) r
ORDER BY s.score DESC;
The result is shown in Fig. :
4. Group ranking
4.1 Rank consecutively in groups
- Use
ROW_NUMBERRealization :
SELECT course_id, score,
ROW_NUMBER() OVER (PARTITION BY course_id ORDER BY score DESC) ranking FROM score;
- Use
VariableandIFStatements for :
SELECT s.course_id, s.score,
IF(@pre_course_id = s.course_id, @cur_rank := @cur_rank + 1, @cur_rank := 1) ranking,
@pre_course_id := s.course_id
FROM score s, (SELECT @cur_rank := 0, @pre_course_id := NULL) r
ORDER BY course_id, score DESC;
The result is shown in Fig. :
4.2 Group side by side jump ranking
- Use
RANKRealization :
SELECT course_id, score,
RANK() OVER(PARTITION BY course_id ORDER BY score DESC)
FROM score;
- Use
VariableandIFStatements for :
SELECT s.course_id, s.score,
IF(@pre_course_id = s.course_id,
@rank_counter := @rank_counter + 1,
@rank_counter := 1) temp1,
IF(@pre_course_id = s.course_id,
IF(@pre_score = s.score, @cur_rank, @cur_rank := @rank_counter),
@cur_rank := 1) ranking,
@pre_score := s.score temp2,
@pre_course_id := s.course_id temp3
FROM score s, (SELECT @cur_rank := 0, @pre_course_id := NULL, @pre_score := NULL, @rank_counter := 1)r
ORDER BY s.course_id, s.score DESC;
The result is shown in Fig. :
4.3 Group parallel continuous ranking
- Use
DENSE_RANKRealization :
SELECT course_id, score,
DENSE_RANK() OVER(PARTITION BY course_id ORDER BY score DESC)
FROM score;
- Use
VariableandIFStatements for :
SELECT s.course_id, s.score,
IF(@pre_course_id = s.course_id,
IF(@pre_score = s.score, @cur_rank, @cur_rank := @cur_rank + 1),
@cur_rank := 1) ranking,
@pre_score := s.score,
@pre_course_id := s.course_id
FROM score s, (SELECT @cur_rank :=0, @pre_score = NULL, @pre_course_id := NULL) r
ORDER BY course_id, score DESC;
The above IF Conditions are extracted :
SELECT s.course_id, s.score,
IF(@pre_score = s.score, @cur_rank, @cur_rank := @cur_rank + 1) temp1,
@pre_score := s.score temp2,
IF(@pre_course_id = s.course_id, @cur_rank, @cur_rank := 1) ranking,
@pre_course_id := s.course_id
FROM score s, (SELECT @cur_rank :=0, @pre_score = NULL, @pre_course_id := NULL) r
ORDER BY course_id, score DESC;
The result is shown in Fig. :
边栏推荐
- flink需求之—SideOutPut(侧输出流的应用:将温度大于30℃的输出到主流,低于30℃的输出到侧流)
- JSCORE day_ 04(7.5)
- JSCORE day_ 05(7.6)
- Detailed explanation of CSRF forged user request attack
- SSRF explanation and burp automatic detection SSRF
- Solve the problem that there is no ado.net entity data model in vs
- Use csrftester to automatically detect CSRF vulnerabilities
- Spark data skew solution
- MySQL - how to determine a field suitable for building an index?
- [SQL注入] 联合查询
猜你喜欢
随机推荐
Golang切片make与new的区别
Based on Flink real-time project: user behavior analysis (III: Statistics of total website views (PV))
05 - 钓鱼网站的攻击与防御
[NPUCTF2020]ezinclude
Spark source code learning - memory tuning
[BJDCTF2020]EzPHP
The difference between golang slice make and new
Dataframe of sparksql
forward和redirect的区别
Rational selection of (Spark Tuning ~) operator
[ciscn2019 finals Day2 web1]easyweb
Spark source code learning - Data Serialization
Detailed explanation of CSRF forged user request attack
2022.7.18DAY608
基于Flink实时项目:用户行为分析(二:实时流量统计)
使用tika 判断文件类型
Data warehouse knowledge points
[RootersCTF2019]I_<3_Flask
MySQL第一篇
golang实现AES有五种加密模式函数,Encrypt加解密字符串输出









![[HFCTF2020]EasyLogin](/img/23/91912865a01180ee191a513be22c03.png)