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 difference: 1. InnoDB supports transactions, ...
1. Time formatting and other methods It is recomm...
Event Description onactivate: Fired when the objec...
Key Takeaways: 1. Mastering CSS3 3D animation 2. ...
1. Elements and tags in HTML <br />An eleme...
This article mainly introduces 6 solutions to the...
MySQL Query Cache is on by default. To some exten...
Search Page: search.wxml page: <view class=&qu...
Introduction Do you really know the difference be...
Table of contents 1. Usage of DATETIME and TIMEST...
title XML/HTML CodeCopy content to clipboard <...
Download url-loader yarn add -D url-loader module...
1.service command The service command actually go...
Demand background The team has the need for integ...
Preface As we all know, the browser's homolog...