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
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.
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.
select @rownum:=@rownum+1 No, foo, bar from table, (SELECT @rownum:=0) r;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With