获取 Postgresql 的最后一个插入 id

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

Get last insert id of Postgresql

postgresql

提问by JoeTidee

As per question:

根据问题:

PHP Postgres: Get Last Insert ID

PHP Postgres:获取上次插入 ID

they have stated that the best way to retrieve the last insert id is:

他们表示,检索最后一个插入 ID 的最佳方法是:

INSERT INTO ....
RETURNING id;

I want to create a wrapper class for Postgresql so that when i run, for example:

我想为 Postgresql 创建一个包装类,以便在我运行时,例如:

$id = $db_wrapper->insert("INSERT...");

that it will always return the last id inserted into variable $id. I do not know the auto_inc column name for queries that will be passed to the insert()function and I would prefer not to have to add "RETURNING id"to the end of all my insert queries - any ideas on how to achieve this?

它将始终返回插入到变量中的最后一个 id $id。我不知道将传递给该insert()函数的查询的 auto_inc 列名称,我不希望不必添加"RETURNING id"到所有插入查询的末尾 - 关于如何实现这一点的任何想法?

回答by manish1706

INSERTwith RETURNING

INSERTRETURNING

INSERT INTO employee (lastname,firstname) VALUES ('Jain', 'Manish') RETURNING id;

I Prefer this because of thousands of traffic at a time on server might get wrong last curval or LASTVAL();Althogh it says this both functions are concurrent and work with individual session but i think the

我更喜欢这个,因为服务器上一次有数千个流量可能会出错 last curval 或LASTVAL(); 尽管它说这两个功能是并发的并且可以与单个会话一起使用,但我认为

RETURNING id

返回 ID

is better and secure.

更好更安全。

Pros: This is very basic and realistic way to get last inserted id, no problem found till the direct entry of 1050 Entry at a time. ;) bit load testing.

优点:这是获取最后插入的 id 的非常基本和现实的方法,直到一次直接输入 1050 Entry 才发现问题。;) 位负载测试。

Cons: Almost none, you might need to modify the way you call your INSERT statement (in the worst case, perhaps your API or DB layer does not expect an INSERT to return a value); it's not standard SQL (who cares);

缺点:几乎没有,您可能需要修改调用 INSERT 语句的方式(在最坏的情况下,您的 API 或 DB 层可能不希望 INSERT 返回值);它不是标准的 SQL(谁在乎);

回答by Always Sunny

PostgreSQL will (by default) create a sequence called 'user_id_seq'. for example if your table name is user. then it is user_id_seq

PostgreSQL 将(默认情况下)创建一个名为“user_id_seq”的序列。例如,如果您的表名是user. 那么它是 user_id_seq

You can then do something like:

然后,您可以执行以下操作:

$strTable = "user";
$last_insert_id = $objPDO->lastInsertId("$strTable_id_seq");

See for multiuser-safemethods http://www.postgresql.org/docs/9.0/interactive/functions-sequence.html

请参阅多用户安全方法http://www.postgresql.org/docs/9.0/interactive/functions-sequence.html

See other ways mysql_insert_id alternative for postgresql

查看mysql_insert_id 替代 postgresql 的其他方式

EDIT: as per comment of Eli

编辑:根据Eli 的评论

//if your table name in model is **user**
$strTable = "user";
$model = new User(); 

// do your stuff
$model->save();

$last_insert_id = $model->lastInsertId("$strTable_id_seq");

OR

或者

If your model is called Model, and has a property called id (aka, the PK of the table), then you can acces this way:

如果您的模型名为 Model,并且有一个名为 id(又名,表的 PK)的属性,那么您可以通过以下方式访问:

//...
$model = new Model();
// do your stuff....
$model->save();

$the_id = $model->id;

回答by Eli

$po_curr_id = PurchaseOrder::select('purchase_orders')->max('id');