简体   繁体   中英

how to SELECT numbering column AFTER order by clause

How can I do a MySQL statement that gives off a numbering column based on the ordered criteria? For example, with the use of variables I have been able to produce such column, but the numbering process occurs before it is ordered so the results look in return... out of order

Try to add a counter:

    SELECT  t.field, t.fieldbis 
            @curRow := @curRow + 1 AS row_number
    FROM    table l
    JOIN    (SELECT @curRow := 0) r
    WHERE   t.field > 50
    ORDER BY t.field2

See With MySQL, how can I generate a column containing the record index in a table?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM