|
|
Question : index column in mySQL select statement
|
|
I would like to select an alias field in a mySQL statement which will contain the current row number of the record.
For example,
I would like to be able to
SELECT [row index from 1... N] AS row_index, A, B, C FROM D;
So I'd get the results such as,
TABLE D row_index A B C 1 x x x 2 x x x 3 x x x .. .. .. N x x x
I would then be able to sort the results in descending order. It's important to note that the row_index alias is created on the fly for any query.
Many thanks,
Dave Kirkby
|
Answer : index column in mySQL select statement
|
|
Not sure why you would want to do this. I would think the easiest way to do this would be create sequence and a temporary table (which has all the same columns plus the sequence) and select records from this table into the temporary table. The only concern I have here is why you would want to do it. The only reason I can see for sorting on the sequence the results are returned is that you think the results will always be returned in the same order as they were inserted. That is probably true; however, the standard for SQL does not require that so at some point later if the search routines find a better way to find matches, the sequence could be totally different. I would suggest it's better to fix the original table to have a sequence number.
|
|
|
|
|