Summarize several common ranking problems in MySQL

Summarize several common ranking problems in MySQL

Preface:

In some application scenarios, we often encounter some ranking problems, such as ranking by grades or age. There are many ways to rank, such as direct ranking, group ranking, ranking with intervals or ranking without intervals, etc. This article will summarize several common ranking problems in MySQL.

Create a test table

create table scores_tb (
 id int auto_increment primary key,
 xuehao int not null, 
 score int not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into scores_tb (xuehao,score) values ​​(1001,89),(1002,99),(1003,96),(1004,96),(1005,92),(1006,90),(1007,90),(1008,94);

# View the inserted datamysql> select * from scores_tb;
+----+--------+-------+
| id | xuehao | score |
+----+--------+-------+
| 1 | 1001 | 89 |
| 2 | 1002 | 99 |
| 3 | 1003 | 96 |
| 4 | 1004 | 96 |
| 5 | 1005 | 92 |
| 6 | 1006 | 90 |
| 7 | 1007 | 90 |
| 8 | 1008 | 94 |
+----+--------+-------+

1. Ordinary ranking

Rank directly by score, starting from 1 and going down, similar to row number. Below we give the query statement and ranking results.

# Query statement SELECT xuehao, score, @curRank := @curRank + 1 AS rank
FROM scores_tb, (
SELECT @curRank := 0
)
ORDER BY score desc;

# Sorting results +--------+-------+------+
| xuehao | score | rank |
+--------+-------+------+
| 1002 | 99 | 1 |
| 1003 | 96 | 2 |
| 1004 | 96 | 3 |
| 1008 | 94 | 4 |
| 1005 | 92 | 5 |
| 1006 | 90 | 6 |
| 1007 | 90 | 7 |
| 1001 | 89 | 8 |
+--------+-------+------+

In the above query statement, we declare a variable @curRank and initialize it to 0. When a row is found, we increment the variable by 1 and use it as the ranking. We can see that there is no gap in this type of ranking and some have the same score but different rankings.

2. Same scores, same rankings, no gap in ranking

# Query statement SELECT xuehao, score, 
CASE
WHEN @prevRank = score THEN @curRank
WHEN @prevRank := score THEN @curRank := @curRank + 1
END AS rank
FROM scores_tb, 
(SELECT @curRank := 0, @prevRank := NULL)
ORDER BY score desc;

# Ranking results+--------+-------+------+
| xuehao | score | rank |
+--------+-------+------+
| 1002 | 99 | 1 |
| 1003 | 96 | 2 |
| 1004 | 96 | 2 |
| 1008 | 94 | 3 |
| 1005 | 92 | 4 |
| 1006 | 90 | 5 |
| 1007 | 90 | 5 |
| 1001 | 89 | 6 |
+--------+-------+------+

3. Tied rankings, with gaps in rankings

Another ranking method is that the same value has the same ranking, and the next rank of the same value should be a jumping integer value, that is, there are gaps in the ranking.

# Query statement SELECT xuehao, score, rank FROM
(SELECT xuehao, score,
@curRank := IF(@prevRank = score, @curRank, @incRank) AS rank, 
@incRank := @incRank + 1, 
@prevRank := score
FROM scores_tb, (
SELECT @curRank := 0, @prevRank := NULL, @incRank := 1
)
ORDER BY score desc) s;
# Ranking results+--------+-------+------+
| xuehao | score | rank |
+--------+-------+------+
| 1002 | 99 | 1 |
| 1003 | 96 | 2 |
| 1004 | 96 | 2 |
| 1008 | 94 | 4 |
| 1005 | 92 | 5 |
| 1006 | 90 | 6 |
| 1007 | 90 | 6 |
| 1001 | 89 | 8 |
+--------+-------+------+

The three ranking methods introduced above are relatively complicated to implement. Fortunately, MySQL 8.0 has added window functions, and the above ranking can be easily achieved using built-in functions.

MySQL 8.0 uses window functions to implement ranking

In MySQL 8.0, you can use the three window functions ROW_NUMBER(), DENSE_RANK(), and RANK() to implement the above three rankings. One thing to note is that the alias after as must not be the same as the previous function name, otherwise an error will be reported. The following are examples of these three functions implementing rankings:

# Three statements for the above three rankings select xuehao,score, ROW_NUMBER() OVER(order by score desc) as row_r from scores_tb;
select xuehao,score, DENSE_RANK() OVER(order by score desc) as dense_r from scores_tb;
select xuehao,score, RANK() over(order by score desc) as r from scores_tb;

# One statement can also query different rankings SELECT xuehao,score,
 ROW_NUMBER() OVER w AS 'row_r',
 DENSE_RANK() OVER w AS 'dense_r',
 RANK() OVER w AS 'r'
FROM `scores_tb`
WINDOW w AS (ORDER BY `score` desc);

# Ranking results+--------+-------+-------+---------+---+
| xuehao | score | row_r | dense_r | r |
+--------+-------+-------+---------+---+
| 1002 | 99 | 1 | 1 | 1 |
| 1003 | 96 | 2 | 2 | 2 |
| 1004 | 96 | 3 | 2 | 2 |
| 1008 | 94 | 4 | 3 | 4 |
| 1005 | 92 | 5 | 4 | 5 |
| 1006 | 90 | 6 | 5 | 6 |
| 1007 | 90 | 7 | 5 | 6 |
| 1001 | 89 | 8 | 6 | 8 |
+--------+-------+-------+---------+---+

Summarize:

This article provides SQL statements for implementing statistical ranking in three different scenarios. You can select the appropriate ranking solution based on different business needs. Compared with MySQL 8.0, we find that ranking can be achieved more easily by using window functions. In fact, business requirements are much more complicated than the examples we have given. It still takes time to accumulate experience to implement such business requirements using SQL.

The above is a detailed summary of several common ranking issues in MySQL. For more information about MySQL ranking, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • MYSQL implements ranking and querying specified user ranking function (parallel ranking function) example code
  • Mysql sorting to get ranking example code
  • MySQL page access statistics and rankings
  • How to add ranking to custom field query results in MySQL
  • MySQL grouping to get the first few records in each group (ranking) with research on group by and order by

<<:  Use of Linux tr command

>>:  Two ways to implement Vue users to log out to the login page without operation for a long time

Recommend

How to set background color and transparency in Vue

Background color and transparency settings As sho...

UDP simple server client code example

I won’t go into details about the theory of UDP. ...

Basic installation process of mysql5.7.19 under winx64 (details)

1. Download https://dev.mysql.com/downloads/mysql...

Detailed explanation of Vue development Sort component code

Table of contents <template> <ul class=&...

How to view the IP address of Linux in VMware virtual machine

1. First, double-click the vmware icon on the com...

Solution to the failure of loading dynamic library when Linux program is running

Unable to load dynamic library under Linux When t...

Detailed explanation of prototypes and prototype chains in JavaScript

Table of contents Prototype chain diagram Essenti...

Detailed process of installing the docker plugin in IntelliJ IDEA (2018 version)

Table of contents 1. Development Environment 2. I...

Introduction to HTML link anchor tags and their role in SEO

The <a> tag is mainly used to define links ...

Monitor changes in MySQL table content and enable MySQL binlog

Preface binlog is a binary log file, which record...

Docker container connection implementation steps analysis

Generally speaking, after the container is starte...

No-nonsense quick start React routing development

Install Enter the following command to install it...

Explain TypeScript mapped types and better literal type inference

Table of contents Overview Using mapped types to ...