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;
Comments: