MySQL uses custom sequences to implement row_number functions (detailed steps)

MySQL uses custom sequences to implement row_number functions (detailed steps)

After reading some articles, I finally figured out how to implement row_number() sorting in MySQL.

Without further ado, let me show you the code:

Step 1: Create a table:

create table grades(
`name` varchar(10),
`subject` varchar(10),
`score` int(10)
)

Step 2: Write data

insert into grades(name, subject, score)
values('Xiao Ming', 'Chinese', 85),
('Xiaohua', 'Chinese', 89),
('Xiao Li', 'Chinese', 91),
('Xiaofang', 'Chinese', 93),
('Xiao Ming', 'Mathematics', 77),
('Xiaohua', 'Mathematics', 95),
('Xiao Li', 'Mathematics', 83),
('Xiaofang', 'Mathematics', 88),
('Xiao Ming', 'English', 90),
('Xiaohua', 'English', 92),
('Xiao Li', 'English', 85),
('Xiaofang', 'English', 88)

The data is as follows:

Step 3:
Requirement: Find the second student in each subject

First, sort:

select name, subject, score 
from grades
order by subject, score desc

The data is as follows:

Then, each subject is sorted by group

select (@i:=case when @subject_pre=t1.subject then @i+1 else 1 end) as rn,
t1.*,
(@subject_pre:=subject) 
from (
    select name, subject, score 
    from grades
    order by subject, score desc
) t1, 
(select @i:=0, @subject_pre:='') as t2
group by subject, score
order by subject, score desc 

To explain:
Add a comparison item subject_pre to record what the previous subject was.
Add a self-increasing sequence to implement the index+1 function.
Because the data is already in order, if the subject pointed to is the same as the previous subject stored, the sequence number is increased by 1, otherwise the sequence number is recalculated from 1.
This achieves group sorting.

Finally, take out the data of rn=2

select name, subject, score from(
select (@i:=case when @subject_pre=t1.subject then @i+1 else 1 end) as rn,
t1.name,
t1.subject,
t1.score,
(@subject_pre:=subject) 
from (
select name, subject, score 
from grades
order by subject, score desc
) t1, 
(select @i:=0, @subject_pre:='') as t2
group by subject, score
order by subject, score desc
)
where rn=2

The final result is as follows:

In this way, the row_number() function is implemented using mysql.

A lot of the information I found on the Internet is not clearly written, so here I use an example to explain this implementation clearly. I hope it will be helpful to you!

This is the end of this article about how to use custom sequences in MySQL to implement the row_number function. For more information about the MySQL row_number function, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed usage of MYSQL row_number() and over() functions
  • Implementation process of row_number in MySQL
  • Usage and precautions of Mysql row number() sorting function

<<:  Detailed explanation of the principles of Vue's responsive system

>>:  Html page supports dark mode implementation

Recommend

Detailed explanation of Nginx forwarding socket port configuration

Common scenarios for Nginx forwarding socket port...

TypeScript learning notes: type narrowing

Table of contents Preface Type Inference Truth va...

HTML table tag tutorial (13): internal border style attributes RULES

RULES can be used to control the style of the int...

Several situations where div is covered by iframe and their solutions

Similar structures: Copy code The code is as foll...

What magical uses does CSS filter have

background Basic Concepts CSS filter property app...

Writing daily automatic backup of MySQL database using mysqldump in Centos7

1. Requirements: Database backup is particularly ...

linux exa command (better file display experience than ls)

Install Follow the README to install The document...

Detailed explanation of the principle of creating tomcat in Eclipse

When creating a tomcat server on a local eclipse,...

How to set MySQL foreign keys for beginners

Table of contents The role of foreign keys mysql ...

How to set mysql5.7 encoding set to utf8mb4

I recently encountered a problem. The emoticons o...

JavaScript imitates Jingdong magnifying glass effect

This article shares the specific code for JavaScr...

Pure CSS to adjust Div height according to adaptive width (percentage)

Under the requirements of today's responsive ...

Practical way to build selenium grid distributed environment with docker

Recently, I needed to test the zoom video confere...

Detailed explanation of vue simple notepad development

This article example shares the specific code of ...