mysql:クエリー結果に自己増分列を追加

657 ワード

クエリー結果に自己増分列を追加
2つの文を調べます.
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