Reverse the “natural order” of a MySQL table without ORDER BY?

前端 未结 2 1619
醉话见心
醉话见心 2021-01-14 09:51

I\'m dealing with a legacy database table that has no insertion date column or unique id column, however the natural order of insertion is still valid when examined with a s

2条回答
  •  礼貌的吻别
    2021-01-14 10:51

    Use @rownum in your query to number each row and then order by the @rownum desc. Here's an example.

    select @rownum:=@rownum+1 ‘rank’, p.* from player p, (SELECT @rownum:=0) r order by score desc limit 10;
    

    Finally, beware that relying on the current order being returned long-term isn't recommended.

提交回复
热议问题