itai February 2016

Column containning row numbers in MYSQL

How do I display a column with the correct row number in my table?

I know that there was a command in SQL called ROW_COUNT but I didn't find anything about it in MYSQL.

An example of what I want can be found here http://www.w3schools.com/sql/sql_and_or.asp

The table automatically gives a CustomerID in a new row.

Answers


dgig February 2016

This you'll want and have to do by incrementing a variable and echoing it in the correct column with your scripting language. There is no way to get SQL to return a "row number" as far as I know.

ROW_COUNT is more for telling you how many rows got affected by an UPDATE I believe.


PierreDuc February 2016

There is a cute little trick which might help. Although mileage may vary with complex queries. Perhaps it will help you out

SET @rownumber = 0;

SELECT 
    @rownumber := @rownumber + 1 AS rownumber,
    t.*
FROM 
    tablename AS t;

Post Status

Asked in February 2016
Viewed 1,754 times
Voted 9
Answered 2 times

Search




Leave an answer