I am trying to get a sequential number column returned with my sql query. I need this to be inside the SELECT statement because I want to nest this query in another, and the
My favorite way to do what several other people have suggested is this:
SELECT @rownum := (IFNULL(@rownum, 0) + 1), my_awesome_table.*
FROM my_awesome_table
You should be careful using user variables in your SELECT, though - As the docs note,
"In SELECT @a, @a:=@a+1, ..., you might think that MySQL will evaluate @a first and then do an assignment second. However, changing the statement (for example, by adding a GROUP BY, HAVING, or ORDER BY clause) may cause MySQL to select an execution plan with a different order of evaluation."
Different versions of MySQL may produce different results. Test accordingly, and consider numbering your rows in your code, rather than in the query.