php SQL - 插入并捕获 id 自动递增值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/621369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:17:21  来源:igfitidea点击:

SQL - INSERT and catch the id auto-increment value

phpsqlauto-increment

提问by Kaptah

What is the best way to get the auto-id value in the same SQL with a SELECT?

使用 SELECT 在同一 SQL 中获取 auto-id 值的最佳方法是什么?

A forum said adding this "; has Return Scope_Identity()"
in the end of the SQL works in ASP.

一个论坛说 在 ASP 中 SQL 工作的末尾添加这个“ ; has Return Scope_Identity()

Is there a corresponding way in PHP?

PHP中有相应的方法吗?

采纳答案by Matthew

It depends on your database server. Using MySQL, call mysql_insert_id()immediately after your insert query. Using PostgreSQL, first query "select nextval(seq)" on the sequence and include the key in your insert query.

这取决于您的数据库服务器。使用 MySQL,mysql_insert_id()在插入查询后立即调用。使用 PostgreSQL,首先select nextval(seq)在序列上查询“ ”,并在插入查询中包含键。

Querying for "select max(id) + 1 from tbl" could fail if another request inserts a record simultaneously.

select max(id) + 1 from tbl如果另一个请求同时插入一条记录,则查询“ ”可能会失败。

回答by Omar Qureshi

In postgres the best way is to do something like:

在 postgres 中,最好的方法是执行以下操作:

insert into foos(name) values ('my_foo') returning id;

回答by Milan Babu?kov

It depends on the database engine you are using. Some DBMS, like Firebirdfor example, have RETURNING clause you can add to your query. For example, if you have a table named TABLE1 with autoincrement column named ID, you can use this:

这取决于您使用的数据库引擎。某些 DBMS(例如Firebird)具有可以添加到查询中的 RETURNING 子句。例如,如果您有一个名为 TABLE1 的表,其自动增量列名为 ID,则可以使用以下命令:

insert into TABLE1(columns...) values (values...) returning ID;

And it would return the inserted ID just like a regular select statement.

它会像普通的 select 语句一样返回插入的 ID。

回答by MyItchyChin

In Microsoft Transact SQL you can use @@IDENTITY.

在 Microsoft Transact SQL 中,您可以使用 @@IDENTITY。

e.g.

例如

DECLARE @Table TABLE ( col0 INT IDENTITY, col1 VARCHAR(255), col2 VARCHAR(255))

INSERT INTO @Table (col1, col2) VALUES ('Hello','World!')

SELECT @@Identity

SELECT * FROM @Table

回答by HLGEM

In SQL Server a insert using the select statement can have an output clause which will return the identity value and whatever other columns you might need to identify which identity goes to which record. If you are using a values clause, then use select scope_identity () immediately after the insert.

在 SQL Server 中,使用 select 语句的插入可以有一个输出子句,该子句将返回标识值以及您可能需要标识哪个标识转到哪个记录的任何其他列。如果您使用的是 values 子句,则在插入后立即使用 select scope_identity()。

回答by A-K

Be very careful: Apparently select nextval(seq) does not work in high concurrency - some other connection can insert between the time when you inserted and the time when you called select nextval(seq). Always test such code in high concurrency test harnesses.

非常小心:显然 select nextval(seq) 在高并发下不起作用 - 在您插入的时间和您调用 select nextval(seq) 的时间之间可以插入一些其他连接。始终在高并发测试工具中测试此类代码。

回答by superUntitled

In php: mysql_insert_id() http://us3.php.net/mysql_insert_id

在 php: mysql_insert_id() http://us3.php.net/mysql_insert_id

or

或者

If you wanted to genterate the number from your mySql select query, you could use this EDIT:

如果你想从你的 mySql 选择查询中生成数字,你可以使用这个 编辑:

SELECT LAST_INSERT_ID(`1`) + 1 FROM table