颠倒“自然秩序",反之亦然.没有ORDER BY的MySQL表? [英] Reverse the "natural order" of a MySQL table without ORDER BY?

查看:50
本文介绍了颠倒“自然秩序",反之亦然.没有ORDER BY的MySQL表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的是没有插入日期列或唯一id列的旧数据库表,但是当使用显示最旧到最新的简单SELECT *检查时,插入的自然顺序仍然有效.

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 simple SELECT * showing oldest to newest.

我想通过分页获取该数据,但将顺序颠倒为ORDER BY date DESC

I'd like like to fetch that data with pagination but reverse the order as if it was ORDER BY date DESC

我已经考虑过将查询包装起来,为结果行分配一个数字ID,然后对结果进行ORDER BY,但哇,这似乎很疯狂.

I've thought about wrapping the query, assigning a numeric id to the resulting rows and then do an ORDER BY on the result but wow that seems crazy.

我正在忽略一个更简单的解决方案吗?

Is there a more simple solution I am overlooking?

我无法将列添加到现有表中,我必须按原样使用它.

I cannot add columns to the existing table, I have to work with it as is.

感谢任何想法!

推荐答案

在查询中使用@rownum为每行编号,然后按@rownum desc进行排序.这是示例.

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.

这篇关于颠倒“自然秩序",反之亦然.没有ORDER BY的MySQL表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆