How to automatically number the results of MYSQL query data

How to automatically number the results of MYSQL query data

Preface

In fact, I have never encountered this kind of situation where the results are numbered during a query before. It was only when my colleague was planning to change jobs and asked about this situation during the interview that I thought of studying it. The following is an analysis of a single table query as an example:

SQL:

SELECT (@i:=@i+1) i,user_id,user_name FROM `dt_user_all_orders`, (SELECT @i:=0) as i WHERE user_name='qqqqqqqqqqq' LIMIT 0,10;

result:

If you need to group and then display the sequence number:

SELECT drug_productor,@y:=@y+1 as num FROM( SELECT drug_productor FROM ts_drug a GROUP BY drug_productor) c,(SELECT @y:=0) d

result:

analyze:

At the beginning, a variable i is defined, and each increment of it results in ➕1, @i:=1;

Here we review the way MySQL defines user variables: select @ variable name

There are two ways to assign values ​​to user variables, one is to use the "=" sign directly, and the other is to use the ":=" sign. The difference is that when using the set command to assign values ​​to user variables, both methods can be used; when using the select statement to assign values ​​to user variables, only the ":=" method can be used, because in the select statement, the "=" sign is regarded as a comparison operator.

(@i:=@i+1) can also be written as @i:=@i+1 . The brackets are added to make the structure clearer visually. After defining a variable, it will be incremented for each query. However, we do not need to increment the variable each time we execute a query statement to obtain results, so we need to reset it to 0. We can use (SELECT @i:=0) as i after the table name with a comma. The reason why as i is used in this way is that the derived table must have an alias. This is its alias, which can be any character.

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:
  • An example of how to query data in MySQL and update it to another table based on conditions
  • How to export Mysql query data to a file using Python
  • Analysis of MySQL Union merge query data and table alias and field alias usage
  • Java implements the example code of connecting to MySQL database unit test query data
  • PHP basics: connecting to MySQL database and querying data
  • mysql query database stored procedures and functions statement
  • MySQL random query data and random update data implementation code
  • MySQL query data by hour, fill in 0 if there is no data

<<:  js implements a simple shopping cart module

>>:  How to monitor and delete timed out sessions in Tomcat

Recommend

Vue state management: using Pinia instead of Vuex

Table of contents 1. What is Pinia? 2. Pinia is e...

Implementing a simple timer in JavaScript

This article example shares the specific code of ...

Example code of implementing starry sky animation with CSS3 advanced LESS

This article introduces the sample code of advanc...

How to call the browser sharing function in Vue

Preface Vue (pronounced /vjuː/, similar to view) ...

JavaScript event capture bubbling and capture details

Table of contents 1. Event Flow 1. Concept 2. DOM...

A simple example of how to implement fuzzy query in Vue

Preface The so-called fuzzy query is to provide q...

Detailed installation tutorial for MySQL zip archive version (5.7.19)

1. Download the zip archive version from the offi...

How to install Elasticsearch7.6 cluster in docker and set password

Table of contents Some basic configuration About ...

How to configure Linux CentOS to run scripts regularly

Many times we want the server to run a script reg...

Explore JavaScript prototype data sharing and method sharing implementation

Data Sharing What kind of data needs to be writte...

Vue project implements file download progress bar function

There are two common ways to download files in da...

Example of how to identify the user using a linux Bash script

It is often necessary to run commands with sudo i...

VMware configuration hadoop to achieve pseudo-distributed graphic tutorial

1. Experimental Environment serial number project...