stackoverflow本身有许多解决方案,其目标是使用自动增量字段或时间戳读取表的最后n行:例如,以下查询以降序从名为tab的表中获取最后10个记录名为id的字段值,它是表中的自动增量字段:
Select * from tab order by id desc limit 10
我的问题是:是否有任何替代方法,而无需获得自动增量字段或时间戳来完成任务以获得相同的输出?
提示:提出这个问题的动机来自以下事实:我们将记录存储到表中,并在使用简单查询查询数据库时未指定任何标准,例如:
Select * from tab
In the sql world,order is not an inherent property of a set of data.
Thus,you get no guarantees from your RDBMS that your data will come
back in a certain order — or even in a consistent order — unless you
query your data with an ORDER BY clause.
因此,如果您没有按某些ID或某些列排序的数据,则无法根据其排序跟踪数据.因此无法保证MysqL如何存储数据,因此您无法获得最后n条记录.
您还可以查看此article:
Caveats
Ordering of Rows
In the absence of ORDER BY,records may be returned in a different
order than the prevIoUs MEMORY implementation.This is not a bug. Any application relying on a specific order without
an ORDER BY clause may deliver unexpected results. A specific order
without ORDER BY is a side effect of a storage engine and query
optimizer implementation which may and will change between minor MysqL
releases.