PHP Postgres:获取最后插入ID

编程入门 行业动态 更新时间:2024-10-24 20:19:52
本文介绍了PHP Postgres:获取最后插入ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

本文发布于:2023-10-15 02:37:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1493006.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PHP   Postgres   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!