Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

php - Getting last inserted UniqueId from MySQL

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

How to retrieve last inserted uniqueId from MySQL?

Tnx

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...