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:
|
<<: 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
The company had a well-configured server that was...
The Swap partition of the Linux system, that is, ...
Table of contents What are immutable values? Why ...
1. Changes in MySQL's default storage engine ...
Table of contents 1. The origin of tomcat 1. Tomc...
MAC installs mysql8.0, the specific contents are ...
1. Goal: Change the value of character_set_server...
Table of contents 1. Use the warehouse to create ...
Overview One of the most powerful features of MyS...
This article shares the specific code of JavaScri...
This article uses an example to share with you a ...
Storage rules for varchar In versions below 4.0, ...
Use ifnull instead of isnull isnull is used to de...
Talk about the scene Send Email Embedding HTML in...
Table of contents 1. Introduction 2. Introduction...