Core code -- Below I will demonstrate the implementation of the sort column in MySQL -- test data CREATE TABLE tb ( score INT ); INSERT tb SELECT 5 UNION ALL SELECT 4 UNION ALL SELECT 4 UNION ALL SELECT 4 UNION ALL SELECT 3 UNION ALL SELECT 2 UNION ALL SELECT 1; --1. row_number sorting SET @row_number =0; SELECT @row_number := @row_number+1 AS row_number,score FROM tb ORDER BY score DESC ; +------------+-------+ | row_number | score | +------------+-------+ | 1 | 5 | | 2 | 4 | | 3 | 4 | | 4 | 4 | | 5 | 3 | | 6 | 2 | | 7 | 1 | +------------+-------+ --2. dense_rank sorting SET @dense_rank = 0, @prev_score = NULL; SELECT @dense_rank :=IF(@prev_score=score,@dense_rank,@dense_rank+1) AS decnse_rank, @prev_score := score AS score FROM tb ORDER BY score DESC ; +-------------+-------+ |decns_rank | score | +-------------+-------+ | 1 | 5 | | 2 | 4 | | 2 | 4 | | 2 | 4 | | 3 | 3 | | 4 | 2 | | 5 | 1 | +-------------+-------+ --3. Rank sorting SET @row=0,@rank=0,@prev_score=NULL; SELECT @row:=@row+1 AS ROW, @rank:=IF(@prev_score=score,@rank,@row) AS rank, @prev_score:=score AS score FROM tb ORDER BY score DESC; +------+------+-------+ | ROW | rank | score | +------+------+-------+ | 1 | 1 | 5 | | 2 | 2 | 4 | | 3 | 2 | 4 | | 4 | 2 | 4 | | 5 | 5 | 3 | | 6 | 6 | 2 | | 7 | 7 | 1 | +------+------+-------+ You may also be interested in:
|
<<: vite2.x implements on-demand loading of ant-design-vue@next components
>>: How to import/save/load/delete images locally in Docker
01. Command Overview Linux provides a rich help m...
React Native can develop iOS and Android native a...
background Getting the slow query log from mysql....
Table of contents 01 Introduction to GTID 02 How ...
<div id="root"> <h2>Keep go...
Layout part: <div id="slider"> &l...
Table of contents 1 Node.js method of sending ema...
Websites without https support will gradually be ...
1. Using it with redis will cause Netty startup c...
Table of contents 1. Master-slave replication Mas...
This article example shares the specific code of ...
Implementation Preparation # Need to back up the ...
I have been having this problem recently when desi...
1. Use the speed control function to control the ...
Preface: position:sticky is a new attribute of CS...