MySQL查询行号可以用变量实现,当然如果你用mysql5的话,可以写个函数实现,下面就让我们来了解一下MySQL查询行号的实现方法。
- mysql> select * from t;
- +------+
- | s1 |
- +------+
- | 6 |
- | 6 |
- | 0 |
- | 19 |
- | 19 |
- | 1 |
- | 2 |
- | 3 |
- | 4 |
- | 0 |
- | 1 |
- | 2 |
- | 4 |
- +------+
- 13 rows in set (0.44 sec)
- mysql> set @rownum=0;
- Query OK, 0 rows affected (0.00 sec)
- mysql> select @rownum:=@rownum+1 as rownum,s1 from t;
- +--------+------+
- | rownum | s1 |
- +--------+------+
- | 1 | 6 |
- | 2 | 6 |
- | 3 | 0 |
- | 4 | 19 |
- | 5 | 19 |
- | 6 | 1 |
- | 7 | 2 |
- | 8 | 3 |
- | 9 | 4 |
- | 10 | 0 |
- | 11 | 1 |
- | 12 | 2 |
- | 13 | 4 |
- +--------+------+
- 13 rows in set (0.04 sec)
以上就是用变量实现MySQL查询行号的方法。
【编辑推荐】