MySQL uses variables to implement various sorting

MySQL uses variables to implement various sorting

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:
  • MySQL 8.0.12 installation and environment variable configuration tutorial under win10
  • Detailed explanation of two methods for setting global variables and session variables in MySQL
  • MySQL 5.6.23 Installation and Configuration Environment Variables Tutorial
  • MySQL 8 new features: how to modify persistent global variables
  • Code analysis of user variables in mysql query statements
  • MySQL variable declaration and stored procedure analysis
  • Several important MySQL variables
  • A brief discussion on the difference between declare and set variables in MySQL stored procedures
  • MySQL variable principles and application examples

<<:  vite2.x implements on-demand loading of ant-design-vue@next components

>>:  How to import/save/load/delete images locally in Docker

Recommend

JavaScript function detailed introduction

Any number of statements can be encapsulated thro...

Pure CSS custom multi-line ellipsis problem (from principle to implementation)

How to display text overflow? What are your needs...

HTML form tag tutorial (2):

This tutorial introduces the application of vario...

In-depth understanding of the creation and implementation of servlets in tomcat

1. What is a servlet 1.1. Explain in official wor...

A brief discussion on the specific use of viewport in mobile terminals

Table of contents 1. Basic Concepts 1.1 Two kinds...

Interpretation and usage of various React state managers

First of all, we need to know what a state manage...

Automatically log out inactive users after login timeout in Linux

Method 1: Modify the .bashrc or .bash_profile fil...

Steps to deploy Docker project in IDEA

Now most projects have begun to be deployed on Do...

Detailed explanation of CSS counter related attributes learning

The CSS counter attribute is supported by almost ...

Nodejs uses readline to prompt for content input example code

Table of contents Preface 1. bat executes js 2. T...

Detailed tutorial on installation and configuration of MySql 5.7.17 winx64

1. Download the software 1. Go to the MySQL offic...

Tutorial on deploying jdk and tomcat on centos7 without interface

1. Install xshell6 2. Create a server connection ...