MYSQL:SQL查询以获取autoincrement字段的值

编程入门 行业动态 更新时间:2024-10-26 16:31:52
本文介绍了MYSQL:SQL查询以获取autoincrement字段的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一张桌子.主键为id,其自动递增. 现在,当我插入新记录时,我需要获取用于更新记录的ID. 我怎样才能做到这一点?如果我使用查询...

I have a table. The primary key is id and its auto incremented. Now when I insert a new record, I need to get the id with which the record was updated. How can I do that? If i use the query...

select max(id) from table_name

...执行后,我可以获得ID.但是我可以确定它是刚刚插入的记录的ID吗?因为很多人会同时使用该应用程序.

...after executing I can get the id. But can I be sure that its the id of the record that was just inserted? Because many people will be using the app at same time.

我正在使用php和mysql.

I am using php and mysql.

Ayone可以在mysql中为我提供一个示例代码片段吗?

Can ayone provie me a sample code snippet in php with mysql?

推荐答案

如果要从自动增量列中查找最新ID,只需运行 mysql_insert_id() 函数生成的ID查询.

If you want to find out what the newest ID from an auto increment column is, you just have to run the mysql_insert_id() function right after your insert query.

像这样:

//--connection already made to database //--now run your INSERT query on the table mysql_query("INSERT INTO table_name (foo_column) VALUES ('everlong')"); //-- right after that previous query, you run this $newest_id = mysql_insert_id();

现在$newest_id将包含插入的最新行的ID.

And now $newest_id will contain the ID of the latest row inserted.

更多推荐

MYSQL:SQL查询以获取autoincrement字段的值

本文发布于:2023-07-09 14:29:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1086989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   MYSQL   SQL   autoincrement

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!