postgresql PHP Postgres:获取上次插入 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6485778/
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-10-20 23:05:51  来源:igfitidea点击:

PHP Postgres: Get Last Insert ID

phpsqlpostgresqlsequencelast-insert-id

提问by Jonah

I found a couple of other questions on this topic. This one...

我发现了关于这个主题的其他几个问题。这个...

mysql_insert_id alternative for postgresql

postgresql 的 mysql_insert_id 替代方案

...and the manualseem to indicate that you can call lastval()any time and it will work as expected. But this one...

...并且手册似乎表明您可以随时致电lastval(),并且会按预期工作。但是这个...

Postgresql and PHP: is the currval a efficent way to retrieve the last row inserted id, in a multiuser application?

Postgresql 和 PHP:在多用户应用程序中,currval 是检索最后一行插入的 id 的有效方法吗?

...seems to state that it has to be within a transaction. So my question is this: can I just wait as long as I like before querying for lastval()(without a transaction)? And is that reliable in the face of many concurrent connections?

...似乎表明它必须在交易中。所以我的问题是:在查询lastval()(没有交易)之前,我可以等多久吗?面对许多并发连接,这是否可靠?

回答by Eelke

INSERT, UPDATE and DELETE in PostgreSQL have a RETURNING clause which means you can do:

PostgreSQL 中的 INSERT、UPDATE 和 DELETE 有一个 RETURNING 子句,这意味着你可以这样做:

INSERT INTO ....
RETURNING id;

Then the query will return the value it inserted for id for each row inserted. Saves a roundtrip to the server.

然后查询将返回它为插入的每一行的 id 插入的值。保存到服务器的往返。

回答by Sjoerd

Yes, the sequence functionsprovide multiuser-safemethods for obtaining successive sequence values from sequence objects.

是的,序列函数提供了多用户安全的方法来从序列对象中获取连续的序列值。