Incrementing a Counter in a MySQL SELECT Query
To generate a sequence number alongside the results of a SELECT query in MySQL, consider the following approach:
Solution:
Employ the following query to achieve the desired output:
select name, @rownum := @rownum + 1 as row_number from your_table cross join (select @rownum := 0) r order by name;
Breaking down the query:
Alternative Syntax:
If the query is used within a stored procedure, it can be expressed in two separate queries:
set @rownum := 0; select name, @rownum := @rownum + 1 as row_number from your_table order by name;
The above is the detailed content of How to Increment a Counter in a MySQL SELECT Query?. For more information, please follow other related articles on the PHP Chinese website!