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

How to set up swap partition SWAP in Linux 7.7

The Swap partition of the Linux system, that is, ...

Explaining immutable values ​​in React

Table of contents What are immutable values? Why ...

Summary of the differences between MySQL storage engines MyISAM and InnoDB

1. Changes in MySQL's default storage engine ...

In-depth analysis of the Tomcat server of Centos 7 system

Table of contents 1. The origin of tomcat 1. Tomc...

MySQL 8.0.11 Installation Guide for Mac

MAC installs mysql8.0, the specific contents are ...

mysql5.7.21 utf8 encoding problem and solution in Mac environment

1. Goal: Change the value of character_set_server...

Linux operation and maintenance basics httpd static web page tutorial

Table of contents 1. Use the warehouse to create ...

The principle and application of MySQL connection query

Overview One of the most powerful features of MyS...

Native JavaScript message board

This article shares the specific code of JavaScri...

Native JS to achieve cool paging effect

This article uses an example to share with you a ...

Some things to note about varchar type in Mysql

Storage rules for varchar In versions below 4.0, ...

A brief discussion on whether MySQL can have a function similar to Oracle's nvl

Use ifnull instead of isnull isnull is used to de...

Solution for converting to inline styles in CSS (css-inline)

Talk about the scene Send Email Embedding HTML in...

vue-cli introduction and installation

Table of contents 1. Introduction 2. Introduction...