Home >Database >Mysql Tutorial >How to add serial number in mysql query
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 to mysql query?
MySQL adds a serial number to the query
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);
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)
(@i:=@i 1) can also be written as
@i:=@i 1, parentheses are added for visual clarity.
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.
(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.
SET @i=0 . Define user variable
i and assign the initial value to
0.
select @variable name, in the above SQL statement, The name of the variable is
i.
"=" directly, and the other is to use
":=". The difference between
= and
:=
SET @Variable name=xxx or
SET @Variable name:=xxx
":= " way, because in the select statement, the
"=" sign is regarded as a comparison operator.
That is:
SELECT @Variable name:=xxx:
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!