MYSQL implements ranking and querying specified user ranking function (parallel ranking function) example code

MYSQL implements ranking and querying specified user ranking function (parallel ranking function) example code

Preface

This article mainly introduces the relevant content about MYSQL to realize ranking and query the specified user ranking function (parallel ranking function), and shares it for your reference and learning. Let's take a look at the detailed introduction.

Table structure:

CREATE TABLE test.testsort (
 
id int(11) NOT NULL AUTO_INCREMENT,
 
uid int(11) DEFAULT 0 COMMENT 'user id',
 
score decimal(10, 2) DEFAULT 0.00 COMMENT 'score',
 
PRIMARY KEY (id)
 
)
 
ENGINE = INNODB
 
AUTO_INCREMENT = 1
 
CHARACTER SET utf8
 
COLLATE utf8_general_ci
 
COMMENT = 'Test sorting'
 
ROW_FORMAT = DYNAMIC;

Idea: You can sort first and then number the results; you can also query the results first and then sort and number them.

illustrate:

@rownum := @rownum + 1 In this sentence, := is used for assignment. This means that @rownum + 1 is executed first, and then the value is assigned to @rownum.

(SELECT @rownum := 0) r This sentence means setting the initial value of the rownum field to 0, that is, the numbering starts from 1.

Achieve ranking:

Method 1:

SELECT t.*, @rownum := @rownum + 1 AS rownum
 
FROM (SELECT @rownum := 0) r, (SELECT * FROM testsort ORDER BY score DESC) AS t;

Method 2:

SELECT t.*, @rownum := @rownum + 1 AS rownum
 
FROM (SELECT @rownum := 0) r, testsort AS t
 
ORDER BY t.score DESC;

result:

View the ranking of a specified user:

Method 1:

SELECT b.* FROM
 
(
 
SELECT t.*, @rownum := @rownum + 1 AS rownum
 
FROM (SELECT @rownum := 0) r,
 
(SELECT * FROM testsort ORDER BY score DESC) AS t
 
) AS b WHERE b.uid = 222;

Method 2:

SELECT b.* from
 
(
 
SELECT t.*, @rownum := @rownum + 1 AS rownum
 
FROM (SELECT @rownum := 0) r, testsort AS t
 
ORDER BY t.score DESC
 
) as b where b.uid = 222;

result:

To achieve a tie ranking (same ranking for the same score):

SELECT
 
obj.uid,
 
obj.score,
 
CASE
 
WHEN @rowtotal = obj.score THEN
 
@rownum
 
WHEN @rowtotal := obj.score THEN
 
@rownum :=@rownum + 1
 
WHEN @rowtotal = 0 THEN
 
@rownum :=@rownum + 1
 
END AS rownum
 
FROM
 
(
 
SELECT
 
uid,
 
score
 
FROM
 
testsort
 
ORDER BY
 
score DESC
 
) AS obj,
 
(SELECT @rownum := 0 ,@rowtotal := NULL)

Query the ranking of a specified user:

SELECT total.* FROM
 
(SELECT
 
obj.uid,
 
obj.score,
 
CASE
 
WHEN @rowtotal = obj.score THEN
 
@rownum
 
WHEN @rowtotal := obj.score THEN
 
@rownum :=@rownum + 1
 
WHEN @rowtotal = 0 THEN
 
@rownum :=@rownum + 1
 
END AS rownum
 
FROM
 
(
 
SELECT
 
uid,
 
score
 
FROM
 
testsort
 
ORDER BY
 
score DESC
 
) AS obj,
 
(SELECT @rownum := 0 ,@rowtotal := NULL) r) AS total WHERE total.uid = 222;

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • Mysql sorting to get ranking example code
  • MySQL page access statistics and rankings
  • How to add ranking to custom field query results in MySQL
  • MySQL grouping to get the first few records in each group (ranking) with research on group by and order by
  • Summarize several common ranking problems in MySQL

<<:  Implementation of Docker building Maven+Tomcat basic image

>>:  Component design specifications for WeChat mini-program development

Recommend

Lombok implementation JSR-269

Preface Introduction Lombok is a handy tool, just...

Summary of commonly used SQL statements for creating MySQL tables

Recently, I have been working on a project and ne...

Super simple qps statistics method (recommended)

Statistics of QPS values ​​in the last N seconds ...

Introduction to the use of CSS3 filter attribute

1. Introduction When writing animation effects fo...

Linux command line quick tips: How to locate a file

We all have files stored on our computers -- dire...

JavaScript flow control (branching)

Table of contents 1. Process Control 2. Sequentia...

A screenshot demo based on canvas in html

Written at the beginning I remember seeing a shar...

Vue.set() and this.$set() usage and difference

When we use Vue for development, we may encounter...

The use of anchor points in HTML_PowerNode Java Academy

Now let's summarize several situations of con...

MySQL 4G memory server configuration optimization

As the number of visits to the company's webs...

Steps to customize icon in Vue

ant-design-vue customizes the use of Ali iconfont...

How to install and configure GitLab on Ubuntu 20.04

introduce GitLab CE or Community Edition is an op...

Detailed explanation of MySql installation and login

Check if MySQL is already installed in Linux sudo...