mysql优化 - 如何高效的获取mysql最后插入数据表中的那条数据的id(该字段自增)?
PHPz
PHPz 2017-04-17 13:18:58
0
4
720

假设:user表,有一个字段id,设置id为自增,执行一次insert语句,如何高效的获取刚刚插入的数据的id是多少?

原来的解决方案:执行insert之后,再执行一次select,找到刚刚插入的数据,拿到id,有没有可能优化这种方案?

PHPz
PHPz

学习是最好的投资!

reply all(4)
Peter_Zhu

Assuming that the poster uses the best language in the world, then you can use the mysql_insert_id() function

刘奇

Hibernate and other persistence tools can obtain the persistent object after saving, and the id inside is the id just inserted.
If there is no persistence layer, you can also use the SQL function LAST_INSERT_ID() to obtain the ID value of the AUTO_INCREACE field just inserted. The previous SQL must be an INSERT statement. If it is another statement, the returned ID value is zero.

黄舟

https://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

Read comments

PHPzhong

Only talk about mysql

SELECT LAST_INSERT_ID();

Some database drivers will provide specialized methods, such as PDO::lastInsertId

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!