Jake Wilson
Jake Wilson

Reputation: 91213

MySQL - row number in recordset?

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...

Upvotes: 3

Views: 2104

Answers (2)

user319198
user319198

Reputation:

select @n := @n + 1 mynum, t.*
from (select @n:=0) initvars, tbl t

Upvotes: 2

Nicola Cossu
Nicola Cossu

Reputation: 56397

select table.*,@rn:=@rn+1 as row_num
from table,(select @rn:=0) as r order by field_you_like

Upvotes: 12

Related Questions