mysql:为查询结果增加一个自增列

在查询结果中增加一个自增列

两句查完:

set @rownum=0;

select (@rownum:=@rownum+1),colname from [tablename or (subquery) a];

一句查完:

select @rownum:=@rownum+1,colnum from (select @rownum:=0) a,[tablename or (subquery) b];

 

案例:

mysql> select @rownum:=@rownum+1 as 'index', name, age from (select @rownum:=0) a, stu;
+-------+-------+------+
| index | name  | age  |
+-------+-------+------+
|     1 | user1 |   20 |
|     2 | lihua |   22 |
|     3 | nini  |   42 |
+-------+-------+------+
3 rows in set (0.00 sec)

 

 

ref link : https://blog.csdn.net/zhenglit/article/details/73824416

你可能感兴趣的:(#,MySQL)