访问mysql中最后插入的行 [英] Accessing last inserted row in mysql
问题描述
在我的数据库服务器上,我正在将数据插入具有自动增量字段"id"的表中.现在,我想在后续步骤中使用最后插入的"id"的值.我可以使用:-
On my db-server i am inserting data in a table having a auto increment field say 'id'. Now i want to use the value of this last inserted 'id' in subsequent steps. I can use this:-
select * from table_name order by id desc limit 1;
但是这里的问题是,这是一台服务器,可能会发生更多的插入操作,并且在某些情况下,我可能会尝试使用我提到的查询来检索数据并获得不同的ID,即.在我的插入和选择之间可能会有其他插入,而我不会得到我插入的值.有什么办法可以解决这个问题?
But the problem here is, it is a server and many more insertions could be happening and there could be a case where i try to retrieve the data with the query i mentioned and get a different id ie. between my insert and select there could be some other insert and i wont get the value i inserted. Any way in which this could be addressed.?
谢谢.
推荐答案
使用此
mysql_insert_id(&mysql);
其基本结构是
mysql_insert_id ([ resource $link_identifier = NULL ] )
检索由上一个查询(通常为INSERT)为AUTO_INCREMENT列生成的ID.
Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).
或在mysql中使用
SELECT LAST_INSERT_ID();
这是参考链接
http://dev.mysql.com/doc/refman/5.0 /en/getting-unique-id.html
http://php.net/manual/en/function.mysql-insert-id .php
这篇关于访问mysql中最后插入的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!