简体   繁体   中英

How can i get the last inserted value of primary kay field?

I am using MySQL.I have to get the last inserted record's primary key value. How can i get this?

This will return the last inserted id.

SELECT LAST_INSERT_ID();

OR

if ((result = mysql_store_result(&mysql)) == 0 &&
    mysql_field_count(&mysql) == 0 &&
    mysql_insert_id(&mysql) != 0)
{
    used_id = mysql_insert_id(&mysql);
}
mysqli_insert_id()
PDO::lastInsertId()
mysql_insert_id()

It's well worth checking the documentation if you are likely to meet "race" conditions. http://uk3.php.net/manual/en/function.mysql-insert-id.php etc.

There are two function that return this value:

mysql_insert_id() --> PHP function

LAST_INSERT_ID() --> MySQL function that can be used by PHP and other Languages or in MySQL command line interface or MySQL front-end

使用以下查询:

  SELECT LAST_INSERT_ID();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM