gpt4 book ai didi

postgresql - 在 Postgres 中选择一行作为 JSON

转载 作者:行者123 更新时间:2023-11-29 11:41:25 25 4
gpt4 key购买 nike

有没有一种直接的方法可以将一个 Postgres 表中的整行放入另一个表中的 JSON 列中?

这里有一个例子来说明我想要做什么。假设我有一张表 people,其中包含姓名、年龄和数据列:

column type
-----------
name text
age int
data json

我想合并到我的 2012_customers 表中,它有更多的列。我如何将所有这些额外的列填充到 people 的 JSON 列中,并将列名保留为数据键?这是一些伪 SQL:

insert into people
select
name,
age,
all_fields_as_json() as json
from customers_2012

最佳答案

在这种情况下,正确的语法是使用 row_to_json(table_name):

insert into people
select
name,
age,
row_to_json(customers_2012) as json
from customers_2012

关于postgresql - 在 Postgres 中选择一行作为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38754420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com