20 Oct 2010 Killerbees   » (Journeyer)

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;

Syndicated 2010-10-20 08:15:00 (Updated 2010-10-20 08:15:56) from Danny Angus

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!