Quantcast
Viewing all articles
Browse latest Browse all 9364

Danny Angus: Note to self, how to get row numbers in a MySQL SELECT

I've often wondered, idly, how to get MySQL to output a row number column, which would rank sorted and grouped results.
Well thanks to this excellent post I've found out how:
SELECT *, @rownum:=@rownum+1 as rank FROM ( 
-- Your Query Goes in Here
SELECT t.*
FROM table t
WHERE something
group by 2,3,4
order by 1 desc limit 20
) day_rank, (SELECT @rownum:=0) r;
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 9364

Trending Articles