Home >Database >Mysql Tutorial >How to add serial number in mysql query

How to add serial number in mysql query

藏色散人
藏色散人Original
2023-02-14 11:04:4111541browse

Mysql query method to add serial numbers: 1. Generate serial numbers by defining user variables, statements such as "SELECT id,userid,subject,score, (@i :=@i 1) AS 'xuhao'FROM tb_score, (SELECT @i := 0) AS itable;"; 2. Add the serial number through the statement "SET @i=0;SELECT id,userid,subject,score...".

How to add serial number in mysql query

##The operating environment of this tutorial: Windows 10 system, MySQL5.7 version, Dell G3 computer.

How to add serial number to mysql query?

MySQL adds a serial number to the query

#Data table
DROP TABLE IF EXISTS tb_score;CREATE TABLE tb_score(
    id INT(11) NOT NULL auto_increment,
    userid VARCHAR(20) NOT NULL COMMENT '用户id',
    subject VARCHAR(20) COMMENT '科目',
    score DOUBLE COMMENT '成绩',
    PRIMARY KEY(id))ENGINE = INNODB DEFAULT CHARSET = utf8;INSERT INTO tb_score(userid,subject,score) VALUES ('001','语文',90);INSERT INTO tb_score(userid,subject,score) VALUES ('001','数学',92);INSERT INTO tb_score(userid,subject,score) VALUES ('001','英语',80);INSERT INTO tb_score(userid,subject,score) VALUES ('002','语文',88);INSERT INTO tb_score(userid,subject,score) VALUES ('002','数学',90);INSERT INTO tb_score(userid,subject,score) VALUES ('002','英语',75.5);INSERT INTO tb_score(userid,subject,score) VALUES ('003','语文',70);INSERT INTO tb_score(userid,subject,score) VALUES ('003','数学',85);INSERT INTO tb_score(userid,subject,score) VALUES ('003','英语',90);INSERT INTO tb_score(userid,subject,score) VALUES ('003','政治',82);

MySQL adds a serial number to the query Add a serial number

A common solution is to generate a serial number by defining user variables

For example: query the data in the table and add the serial number, the corresponding SQL is:

SELECT
	id,userid,subject,score, (@i :=@i + 1) AS '序号'FROM
	tb_score,
	(SELECT @i := 0) AS itable;
+----+--------+---------+-------+--------+
| id | userid | subject | score | 序号   |
+----+--------+---------+-------+--------+
|  1 | 001    | 语文    |    90 |      1 |
|  2 | 001    | 数学    |    92 |      2 |
|  3 | 001    | 英语    |    80 |      3 |
|  4 | 002    | 语文    |    88 |      4 |
|  5 | 002    | 数学    |    90 |      5 |
|  6 | 002    | 英语    |  75.5 |      6 |
|  7 | 003    | 语文    |    70 |      7 |
|  8 | 003    | 数学    |    85 |      8 |
|  9 | 003    | 英语    |    90 |      9 |
| 10 | 003    | 政治    |    82 |     10 |
+----+--------+---------+-------+--------+
10 rows in set (0.00 sec)
SELECT
	*, (@i :=@i + 1) AS '序号'FROM
	tb_score;
+----+--------+---------+-------+--------+
| id | userid | subject | score | 序号   |
+----+--------+---------+-------+--------+
|  1 | 001    | 语文    |    90 |     11 |
|  2 | 001    | 数学    |    92 |     12 |
|  3 | 001    | 英语    |    80 |     13 |
|  4 | 002    | 语文    |    88 |     14 |
|  5 | 002    | 数学    |    90 |     15 |
|  6 | 002    | 英语    |  75.5 |     16 |
|  7 | 003    | 语文    |    70 |     17 |
|  8 | 003    | 数学    |    85 |     18 |
|  9 | 003    | 英语    |    90 |     19 |
| 10 | 003    | 政治    |    82 |     20 |
+----+--------+---------+-------+--------+
10 rows in set (0.00 sec)
or

SET @i=0;SELECT id,userid,subject,score,@i:=@i+1 AS '序号' FROM tb_score;
+----+--------+---------+-------+--------+
| id | userid | subject | score | 序号   |
+----+--------+---------+-------+--------+
|  1 | 001    | 语文    |    90 |      1 |
|  2 | 001    | 数学    |    92 |      2 |
|  3 | 001    | 英语    |    80 |      3 |
|  4 | 002    | 语文    |    88 |      4 |
|  5 | 002    | 数学    |    90 |      5 |
|  6 | 002    | 英语    |  75.5 |      6 |
|  7 | 003    | 语文    |    70 |      7 |
|  8 | 003    | 数学    |    85 |      8 |
|  9 | 003    | 英语    |    90 |      9 |
| 10 | 003    | 政治    |    82 |     10 |
+----+--------+---------+-------+--------+
10 rows in set (0.00 sec)

Explanation

1,

(@i:=@i 1) can also be written as @i:=@i 1, parentheses are added for visual clarity.

It means: add

1 to variable i and assign it to variable i. After defining a variable, it will be given in every query. This variable is incremented by itself. There is no need for this variable to be incremented every time a query statement is executed to obtain results.

2,

(SELECT @i:=0) AS itable, define user variable i, set the initial value to 0, and then It is used as a derived table and AS defines the alias of the table.

3,

SET @i=0 . Define user variable i and assign the initial value to 0.

Related knowledge points

1. How MySQL defines user variables:


select @variable name, in the above SQL statement, The name of the variable is i.

2. User variable assignment: one is to use

"=" directly, and the other is to use ":=". The difference between

= and :=

When using the set command to assign values ​​to user variables, both methods can be used, namely:

SET @Variable name=xxx or SET @Variable name:=xxx

When using the select statement to assign a value to a user variable, you can only use

":= " way, because in the select statement, the "=" sign is regarded as a comparison operator. That is:
SELECT @Variable name:=xxx:

①: User variable

②: Derived table

③: AS setting alias

## Recommended study: "MySQL Video Tutorial

"

The above is the detailed content of How to add serial number in mysql query. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn