Implementation process of row_number in MySQL

Implementation process of row_number in MySQL

1. Background

Generally, in a data warehouse environment, we can easily use the row_number function to group data according to a certain dimension to achieve the effect of sorting the data numbers in each group. As shown in the figure below, this is the effect diagram generated in the MySQL environment. The grouping here is based on lcid, and num is equivalent to the effect achieved by the row_number function:

Implementing row_number effect in mysql environment

2. Implementation process

1. Set mysql variables

Set up two variables

set @row_number:=0; --Generate row_number sequence number according to the judgment result of lcid_no set @lcid_no:= 0; --Used to obtain the lcid column data of each row, and then compare it with the lcid data of the previous row. If they are the same, it will increase by 1, otherwise it will be 1

2. Use case when

SELECT @row_number:=CASE
    WHEN @lcid_no = s.lcid THEN @row_number + 1 
    ELSE 1
    END AS num,
    @lcid_no:=s.lcid AS lcid,
	  s.lcid
FROM r_qcloud_approval_fh_d s,(select @orw_number:=0,@lcid_no:=0) t
ORDER BY s.lcid;

3. Process analysis <br /> If there are multiple identical data
1) Under the initial condition, the cursor points to the first data. At this time, lcid_no = 0, lcid_no is not equal to lcid, so row_number = 1
2) The cursor points to the second data, lcid_no = lcid of the previous data, because the lcid of the previous data = the lcid of the current row, so row_number = 2
No duplicate data
1) Under the initial condition, the cursor points to the first data. At this time, lcid_no = 0, lcid_no is not equal to lcid, so row_number = 1

3. Usage scenarios

In a relational database like MySQL, there is no row_number function.

This is the end of this article about the implementation process of row_number in MySQL. For more relevant content about row_number in MySQL, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed usage of MYSQL row_number() and over() functions
  • PostgreSQL ROW_NUMBER() OVER() usage explanation
  • row_number() and distinct usage in postgreSQL
  • Postgresql rank() over, dense_rank(), row_number() usage differences
  • Detailed examples of common usage of row_number function in SQL Server
  • Introduction to the use of the four major sql ranking functions ROW_NUMBER, RANK, DENSE_RANK, NTILE
  • SQL ROW_NUMBER() and OVER() method case study

<<:  Learn Node.js from scratch

>>:  In-depth explanation of nginx location priority

Recommend

PHP-HTMLhtml important knowledge points notes (must read)

1. Use frameset, frame and iframe to realize mult...

Detailed example of MySQL data storage process parameters

There are three types of MySQL stored procedure p...

JavaScript function syntax explained

Table of contents 1. Ordinary functions 2. Arrow ...

Detailed explanation of mysql user variables and set statement examples

Table of contents 1 Introduction to user variable...

How to configure mysql5.6 to support IPV6 connection in Linux environment

Introduction: This article mainly introduces how ...

1 minute Vue implements right-click menu

Table of contents Rendering Install Code Implemen...

MySQL data aggregation and grouping

We often need to summarize data without actually ...

Markup Languages ​​- What to learn after learning HTML?

Click here to return to the 123WORDPRESS.COM HTML ...

MySQL backup and recovery design ideas

background First, let me explain the background. ...

Real-time refresh of long connection on Vue+WebSocket page

Recently, the Vue project needs to refresh the da...

How to solve the problem that the project in eclipse cannot be added to tomcat

1. Right-click the project and select properties ...

Scary Halloween Linux Commands

Even though it's not Halloween, it's wort...

Vue implements a simple shopping cart example

This article example shares the specific code of ...

Detailed explanation of MySQL slow queries

Query mysql operation information show status -- ...

The whole process record of Vue export Excel function

Table of contents 1. Front-end leading process: 2...