Mysql sorting to get ranking example code

Mysql sorting to get ranking example code

The code looks like this:

SELECT @i:=@i+1 rowNum,
if(@total=t.s_score,@rank,@rank:=@i) rank,@total:=t.s_score,
t.*
from(
select t1.* ,t2.s_score from student t1 LEFT JOIN score t2 on t1.s_id=t2.s_id and t2.c_id="01" ORDER BY t2.s_score desc
)t,(select @i:=0,@rank:=0,@total:=null) s ;
SELECT @i:=@i+1 rowNum,
if(@total=t.s_score,@rank,@rank:=@rank+1) rank,@total:=t.s_score,
t.*
from(
select t1.* ,t2.s_score from student t1 LEFT JOIN score t2 on t1.s_id=t2.s_id and t2.c_id="01" ORDER BY t2.s_score desc
)t,(select @i:=0,@rank:=0,@total:=null) s ;

Mysql gets the ranking after the grades are sorted

In fact, it outputs the row number after mysql sorting

RT: Get the ranking of a single user's score among all users' scores

It can be divided into two steps:

1. Find all users and their performance rankings

select id,maxScore,(@rowNum:=@rowNum+1) as rowNo 
from t_user, 
(select (@rowNum :=0) ) b 
order by t_user.maxScore desc

2. Find out the ranking of a user among all users' scores

select u.rowNo from ( 
select id,(@rowNum:=@rowNum+1) as rowNo 
from t_user, 
(select (@rowNum :=0) ) b 
order by t_user.maxScore desc ) u where u.id="2015091810371700001";

Summarize

The above is the Mysql sorting and ranking acquisition introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • MYSQL implements ranking and querying specified user ranking function (parallel ranking function) 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
  • Summarize several common ranking problems in MySQL

<<:  How to use Webstorm and Chrome to debug Vue projects

>>:  How to deal with the xfs_vm_releasepage warning problem reported by the Linux system

Recommend

Differences between MySQL MyISAM and InnoDB

the difference: 1. InnoDB supports transactions, ...

Summary of common tool functions necessary for front-end development

1. Time formatting and other methods It is recomm...

Detailed description of common events and methods of html text

Event Description onactivate: Fired when the objec...

Sample code for realizing book page turning effect using css3

Key Takeaways: 1. Mastering CSS3 3D animation 2. ...

Summary of learning HTML tags and basic elements

1. Elements and tags in HTML <br />An eleme...

6 solutions to IDEA's inability to connect to the MySQL database

This article mainly introduces 6 solutions to the...

Detailed explanation of the use of MySQL select cache mechanism

MySQL Query Cache is on by default. To some exten...

WeChat applet implements search function and jumps to search results page

Search Page: search.wxml page: <view class=&qu...

Differences between MySQL CHAR and VARCHAR when storing and reading

Introduction Do you really know the difference be...

About the selection of time date type and string type in MySQL

Table of contents 1. Usage of DATETIME and TIMEST...

Example of converting webpack images to base64

Download url-loader yarn add -D url-loader module...