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: 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: 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 explanation of the principles of Vue's responsive system
>>: Html page supports dark mode implementation
Common scenarios for Nginx forwarding socket port...
Table of contents Preface Type Inference Truth va...
RULES can be used to control the style of the int...
Similar structures: Copy code The code is as foll...
background Basic Concepts CSS filter property app...
1. Requirements: Database backup is particularly ...
Install Follow the README to install The document...
When creating a tomcat server on a local eclipse,...
Table of contents The role of foreign keys mysql ...
1. Location regular expression Let's take a l...
I recently encountered a problem. The emoticons o...
This article shares the specific code for JavaScr...
Under the requirements of today's responsive ...
Recently, I needed to test the zoom video confere...
This article example shares the specific code of ...