MySQL - row number in recordset?

2020-03-26 08:46发布

First of all, I'm not asking how to get the current row number after the results have been return to you.

I'm wondering, is it possible to get the row number as one of the returned columns in MySQL results? What I'm trying to do is to add a number to increments up to every single row. Like this:

| id | myNum | name |
+----+-------+------+
| 34 |     1 | John |
| 24 |     2 | Alex |
| 56 |     3 | Brad |
etc...

I'm guessing it would involve stored procedures, but I'm wondering if it's possible without them...

标签: mysql
2条回答
闹够了就滚
2楼-- · 2020-03-26 09:21
select table.*,@rn:=@rn+1 as row_num
from table,(select @rn:=0) as r order by field_you_like
查看更多
▲ chillily
3楼-- · 2020-03-26 09:24
select @n := @n + 1 mynum, t.*
from (select @n:=0) initvars, tbl t
查看更多
登录 后发表回答