Display row number in Mysql Select Query


How many times did want to have the row number or the position of the row in the selected query? I had many cases where I needed this feature. I needed the feature for printing the rank of a person where each row is the mark of the person. So lets see how to implement this in mysql.
There is no straight forward function or query parameter to do it (At least none I know of). So I am writing the query to declare a variable and increment that variable on printing that variable along with printing each row. So it just displays the variable on each row. So here is sql query for it. Try it out and tell me.

 select @rownum:=@rownum+1 ‘rank’, p.* from player p, (SELECT @rownum:=0) r order by score desc limit 10;

, , , , ,

  1. No comments yet.
(will not be published)