简体   繁体   English

使用postgres从最后一个插入查询中获取数据

[英]Get data from the last insert query using postgres

i know there is a way how i can get the whole data from my last insert, including the auto generated fields, like id, and default content. 我知道有一种方法可以从上次插入中获取全部数据,包括自动生成的字段(例如ID)和默认内容。 But the problem is: How can i do this? 但是问题是:我该怎么做?

for eg: 例如:

INSERT INTO schema.table (col1,col2) VALUES ("rowdata1","rowdata2");

the table looks like this: 该表如下所示:

id, col1, col2, col3 (default='t')

so how can i get the value of id and col3? 所以我如何获得id和col3的值? There's a keyword like RETURNING or so, but this throws an error :) 有一个类似RETURNING之类的关键字,但这会引发错误:)

INSERT
INTO    schema.table (col1, col2)
VALUES  ('rowdata1', 'rowdata2')
RETURNING
        *

, or, if you only need specific columns, ,或者,如果您只需要特定的列,

INSERT
INTO    schema.table (col1, col2)
VALUES  ('rowdata1', 'rowdata2')
RETURNING
        id, col3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM