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
In Linux systems, especially server systems, it i...
PSD to div css web page cutting example Step 1: F...
In Linux, when a file is created, the owner of th...
Table of contents Docker image download Start mys...
Table of contents Preface Case: Imitation of JD.c...
Table of contents What are hooks? Class Component...
Table of contents Difference between char and var...
This article mainly introduces the solution to th...
<br />The most common mistake made by many w...
Serve: # chkconfig --list List all system service...
I recently came into contact with MySQL. Yesterda...
Hello everyone, I am Tony, a teacher who only tal...
This article records the installation and configu...
Recently, I have implemented such an effect: clic...
Table of contents Why use setState Usage of setSt...