复制一个表中的值并将其插入另一个jsonb字段

问题描述 投票:1回答:2

请帮助我理解,如何从一个表中复制值并将其插入到jsonb字段中的另一个表中?

additional_info是产品表中的jsonb字段。

dedication, stories, status这个字段来自书籍表我想在additional_info字段中复制。

我试过这样,但显然这是不正确的

INSERT INTO products (order_id, name, price, type, additional_info)

SELECT order_id, name, null, 'Book', '{\"dedication\": dedication, \"stories\": stories, \"status\": status }'

FROM books;
postgresql jsonb
2个回答
1
投票

你需要使用jsonb_build_object\在SQL中没有特殊含义,在SQL字符串文字中不需要\"

INSERT INTO products (order_id, name, price, type, additional_info)
select order_id, 
       name, 
       null, 
       'Book', 
       jsonb_build_object('dedication', dedication, 'stories', stories, 'status', status)
from books

0
投票

像这样的东西。虽然我不确定你是如何使用斜杠“\”的。

INSERT INTO products (order_id, name, price, type, additional_info)

SELECT order_id, name, null, 'Book', json_build_object("dedication\", dedication,  "stories\", stories, "status\", status) AS additional_info

FROM books;
© www.soinside.com 2019 - 2024. All rights reserved.