Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use sorting to get ranking in Mysql

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to use sorting to obtain rankings in Mysql. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

The code is as follows:

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 on t1.s_id=t2.s_id and T2 = "01" ORDER BY t2.s_score desc) t, (select @ iRank Letters / 0Rank Letters / 0Nulls) 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 on t1.s_id=t2.s_id and T2 = "01" ORDER BY t2.s_score desc) t, (select @ iRank Letters / 0Rank Letters / 0Nulls) s

Mysql obtains the ranking after the ranking of scores.

It is actually the sorted line number of the output mysql.

RT: get the ranking of individual users' scores among all users' scores

It can be divided into two steps:

1. Find out 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 user 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". This is about how to use sorting to obtain rankings in Mysql. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report