SELECTした結果に対して先頭などに連番を付けたい場合は、以下のようにSQLを発行することにより実現できます。
以下に2種類方法を記します。
mysql> create table t1 (a int, b int, c int); Query OK, 0 rows affected (0.01 sec)
mysql> insert into t1 values(1,10,100),(2,20,200),(3,30,300); Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0
mysql> set @num := 0; Query OK, 0 rows affected (0.00 sec)
mysql> select (@num := @num + 1) as no, t1.* from t1; +------+------+------+------+ | no | a | b | c | +------+------+------+------+ | 1 | 1 | 10 | 100 | | 2 | 2 | 20 | 200 | | 3 | 3 | 30 | 300 | +------+------+------+------+ 3 rows in set (0.00 sec)
上記では、変数を指定した後、SELECTを実行していました。
以下の例は、1行で実現しています。
mysql> select @num := @num + 10 as no, t1.* from (select @num := 100) as dmy, t1; +------+------+------+------+ | no | a | b | c | +------+------+------+------+ | 110 | 1 | 10 | 100 | | 120 | 2 | 20 | 200 | | 130 | 3 | 30 | 300 | +------+------+------+------+ 3 rows in set (0.00 sec)