MySQL选择没有主ID或日期字段的最后插入的记录 [英] MySQL Select last inserted records without primary ID or date field

查看:63
本文介绍了MySQL选择没有主ID或日期字段的最后插入的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下字段的 MySQL 表:

I have a MySQL table with the following fields:

Email, Flag1, Flag2

问题:有什么办法可以让我选择获取最后 100 个插入的字段?

The question: Is there any way i can do a select to get the last 100 inserted fields?

据我所知,表格是通过在表格末尾添加带有简单插入"的新记录来填充的.

As i understand the table populates by adding new records with simple "insert" to the end of the table.

我知道如何在 PHP+MySQL 中做到这一点(非常丑陋的方式:SELECT ALL RECORDS在 php 循环中为每条记录分配自动递增的 ID,并获得最后 100记录.)

I have an idea how to do that in PHP+MySQL (the very ugly way: SELECT ALL RECORDS, assign to each record autoincremented ID in php loop, and get the last 100 records.)

是否有更优雅的方式使用纯 MySQL 来做到这一点?

推荐答案

那是不可能的,因为如果您不设置带有时间戳或自动增量的字段,系统将如何知道最后插入的内容?.所以你有两个简单的选择.

that's not possible, because if you don't set a field with the timestamp or an autoincrement how will the system know the last inserted?. so you have 2 easy options.

  • 创建时间戳字段:当您下订单时,只需让 select * from yourtamble order by timefield desc limit 100

创建一个带有自动增量的 as ID.这样做的好处是您可以拥有一个主键并知道如何插入列表的确切顺序,坏处是您需要另一个字段时间来知道何时插入数据.作为时间戳解决方案,您可以使 select * from yourtamble order by id desc limit 100

Create a as ID with autoincrement. The good about this is that you can have a primary key and know the exact order how the listing were inserted, the bad thing about this, is that you'll need another field time to know when the data were insert. As the timestamp solution you could make select * from yourtamble order by id desc limit 100

这篇关于MySQL选择没有主ID或日期字段的最后插入的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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