PHP Postgres:获取最后插入ID [英] PHP Postgres: Get Last Insert ID
问题描述
关于这个主题,我发现了其他几个问题。这个...
I found a couple of other questions on this topic. This one...
...和手册似乎表明您可以随时调用 lastval()
,它将按预期运行。但这一个...
...and the manual seem to indicate that you can call lastval()
any time and it will work as expected. But this one...
...似乎表明它必须在事务内。所以我的问题是这样:我可以在查询 lastval()
之前等待我想要的时间吗(没有事务)?面对许多并发连接,这可靠吗?
...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?
推荐答案
PostgreSQL中的INSERT,UPDATE和DELETE有RETURNING子句,这意味着您可以这样做:
INSERT, UPDATE and DELETE in PostgreSQL have a RETURNING clause which means you can do:
INSERT INTO ....
RETURNING id;
然后,查询将为插入的每一行返回其为id插入的值。将往返时间保存到服务器。
Then the query will return the value it inserted for id for each row inserted. Saves a roundtrip to the server.
这篇关于PHP Postgres:获取最后插入ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!