从 MySQL 获取最后插入的 UniqueId [英] Getting last inserted UniqueId from MySQL

查看:48
本文介绍了从 MySQL 获取最后插入的 UniqueId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在插入记录,主键是PHP创建的uniqueId.

I'm inserting records, primary key is a uniqueId created by PHP.

如何从 MySQL 中检索最后插入的 uniqueId?

How to retrieve last inserted uniqueId from MySQL?

Tnx

推荐答案

如果是 auto_incremented 值,您将使用 <代码>mysql_insert_id()(或其mysqli兄弟)插入以获取分配给它的 ID.

If it is an auto_incremented value you would use mysql_insert_id() (or its mysqli sibling) immediately after doing an insert to get the ID assigned to it.

如果你想获得一个非自动递增的值,你需要选择具有最高值的行并按该值以降序排序来完成这项工作(假设它是数字或字母数字并按顺序递增):

If you wanted to get a non-auto incremented value you would need to SELECT the row with the highest value and sort by that value in descending order to do the job (assuming it is numeric or alphanumeric and increments sequentially):

SELECT id FROM tablename ORDER id DESC LIMIT 1

如果它是一个随机 ID,则您需要按日期排序:

If it is a random ID than you would need to sort by date:

SELECT id FROM tablename ORDER datecol DESC LIMIT 1

如果您没有任何其他方法对这些记录进行排序,您可以获得表中的最后一行,但不能保证记录是最新的,因此您的结果没有准确性.

If you don't have any other method of sorting these records you can get the last row in the table but there is no guarantee that record is the newest and thus you have no accuracy in your results.

这篇关于从 MySQL 获取最后插入的 UniqueId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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