gpt4 book ai didi

postgresql - 根据 hstore 列的选择插入

转载 作者:行者123 更新时间:2023-11-29 14:34:53 27 4
gpt4 key购买 nike

尝试将 hstore (postgreql) 中的值插入到更通用的表中

在我的汽车表中,我有这些字段

id
fields (hstore)

我的存储表,我有这些字段

id
key
value
car_id
date

如何在插入键、值到我的存储表中循环到我的字段属性。

有没有办法用选择命令来做到这一点?

最佳答案

示例数据:

insert into car values
(1, 'brand=>ford, color=>yellow'),
(2, 'brand=>volvo, mileage=>50000, year=>2015');

使用函数 each(hstore)获取 hstore 列的 (key, value) 对:

select id, key, value
from car, each(fields);

id | key | value
----+---------+--------
1 | brand | ford
1 | color | yellow
2 | year | 2015
2 | brand | volvo
2 | mileage | 50000
(5 rows)

插入命令可能如下所示:

insert into store (car_id, key, value)
select id, key, value
from car, each(fields);

关于postgresql - 根据 hstore 列的选择插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46671571/

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