postgresql 将 JSON 字符串插入 Postgres 并返回字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39835304/
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
Insert JSON string into Postgres and return field
提问by Michiel Borkent
I want to insert a JSON string into a Postgres table with a jsonb field and want the insert query to return a part of the JSON. For example, I want to return the id in the example below. What goes on the question marks?
我想将一个 JSON 字符串插入到带有 jsonb 字段的 Postgres 表中,并希望插入查询返回 JSON 的一部分。例如,我想返回下面示例中的 id。问号上有什么?
insert into mytable (myjson)
values ('{"id":123}') returning ???
回答by a_horse_with_no_name
Use the ->>
operator to extract the value of the id
attribute:
使用->>
运算符提取id
属性的值:
insert into mytable (myjson)
values ('{"id":123}')
returning (myjson ->> 'id');