Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show sequential number in MySQL query result

Tags:

I have some simple query:

SELECT foo, bar FROM table 

i think you now whats the result looks like.

What I want to do is to show some sequential number based on how many data appear from query result. its just like AUTO_INCREMENT(its not mean i want to show ID). The result what I want is like:

|No|   foo   |    bar   | ------------------------- |1 |   bla   |    123   | |2 |   boo   |    abc   | |3 |   wow   |    xxx   | 

How should I do to make it?

thanks in advance

like image 842
nunu Avatar asked May 19 '11 08:05

nunu


People also ask

How do you find sequential numbers in SQL?

To number rows in a result set, you have to use an SQL window function called ROW_NUMBER() . This function assigns a sequential integer number to each result row. However, it can also be used to number records in different ways, such as by subsets.

Can we use sequence in MySQL?

By default, MySQL will start sequence from 1, but you can specify any other number as well at the time of the table creation. The following program is an example which shows how MySQL will start the sequence from 100.


1 Answers

select @rownum:=@rownum+1 No, foo, bar from table, (SELECT @rownum:=0) r; 
like image 152
bungdito Avatar answered Sep 22 '22 13:09

bungdito