简体   繁体   English

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

[英]SQL - INSERT and catch the id auto-increment value

What is the best way to get the auto-id value in the same SQL with a SELECT?在具有 SELECT 的同一 SQL 中获取自动 ID 值的最佳方法是什么?

A forum said adding this " ; has Return Scope_Identity() "一个论坛说添加这个“ ; has Return Scope_Identity()
in the end of the SQL works in ASP.在 SQL 在 ASP 中工作的末尾。

Is there a corresponding way in PHP? PHP中有对应的方式吗?

It depends on your database server.这取决于您的数据库服务器。 Using MySQL, call mysql_insert_id() immediately after your insert query.使用 MySQL,在插入查询后立即调用mysql_insert_id() Using PostgreSQL, first query " select nextval(seq) " on the sequence and include the key in your insert query.使用 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 ”可能会失败。

In postgres the best way is to do something like:在 postgres 中,最好的方法是执行以下操作:

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

It depends on the database engine you are using.这取决于您使用的数据库引擎。 Some DBMS, like Firebird for example, have RETURNING clause you can add to your query.某些 DBMS,例如Firebird ,具有 RETURNING 子句,您可以将其添加到查询中。 For example, if you have a table named TABLE1 with autoincrement column named ID, you can use this:例如,如果您有一个名为 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。

In Microsoft Transact SQL you can use @@IDENTITY.在 Microsoft Transact SQL 中,您可以使用 @@IDENTITY。

eg例如

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

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).要非常小心:显然 select nextval(seq) 在高并发下不起作用 - 在您插入的时间和调用 select nextval(seq) 的时间之间可以插入一些其他连接。 Always test such code in high concurrency test harnesses.始终在高并发测试工具中测试此类代码。

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.在 SQL 服务器中,使用 select 语句的插入可以具有 output 子句,该子句将返回标识值以及您可能需要识别哪个标识的任何其他列。 If you are using a values clause, then use select scope_identity () immediately after the insert.如果您使用 values 子句,则在插入后立即使用 select scope_identity()。

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 查询中生成数字,你可以使用这个编辑:

SELECT LAST_INSERT_ID(`1`) + 1 FROM table 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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