Get row number (like excel)

时间:2016-07-11 22:48:39

标签: mysql

How would I get the row number of a mysql output, for example:

select * from table

name      age
David     12
Frank     13

I want to get the row number, like so:

select *, row_num from table

row    name      age
1      David     12
2      Frank     13

enter image description here

The row number on the left -- 1 through 7 -- not a part of the data itself.

1 个答案:

答案 0 :(得分:1)

在mysql中,您必须使用User-Defined Variables

SELECT @rowno := @rowno + 1 AS row_no, *
FROM table
CROSS JOIN (SELECT @rowno := 0) t
-- ORDER BY age