MySQL custom row number in a result set

MySQL INDEX

You can add a sequential number as a custom row number of a row using SET clause and increment at next row.

Suppose users table has columns name and age.
When you want to show the result you want to add extra column serial_no.

So using below query you can achieve custom row number:

SET @serial_number = 0;

SELECT
(@serial_number:=@serial_number + 1) AS serial, name, age
FROM
users
LIMIT 0,50;
Serial no Name Age
1 John 25
2 Jane 24
3 Tim 34

For any types of queries, you can contact us on info@namasteui.com.

You may like:  Intro: MySQL Stored Procedures

Leave a Reply

Your email address will not be published. Required fields are marked *