ranking entries in mysql table

后端 未结 5 1275
清歌不尽
清歌不尽 2021-02-06 12:44

I have a MySQL table with many rows. The table has a popularity column. If I sort by popularity, I can get the rank of each item. Is it possible to retrieve the rank of a partic

5条回答
  •  有刺的猬
    2021-02-06 13:09

    If you're doing this using PDO then you need to modify the query to all be within the single statement in order to get it to work properly. See PHP/PDO/MySQL: Convert Multiple Queries Into Single Query

    So hobodave's answer becomes something like:

    SELECT t.*, (@count := @count + 1) as rank
    FROM table t 
    CROSS JOIN (SELECT @count := 0) CONST
    ORDER BY t.popularity;
    

提交回复
热议问题