关于这个主题,我发现了其他几个问题。这个...
I found a couple of other questions on this topic. This one...
PostgreSQL的mysql_insert_id替代
...和手册似乎表明您可以随时调用 lastval(),它将按预期运行。但这一个...
...and the manual seem to indicate that you can call lastval() any time and it will work as expected. But this one...
Postgresql和PHP:这是一种有效的方法
...似乎表明它必须在事务内。所以我的问题是这样:我可以在查询 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
发布评论