2017-10-10 79 views
0

的在尝试从hstore(postgreql)插入值,以一个更通用的表插入基于选择hstore列

在我的车表,我的论文领域

id 
fields (hstore) 

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

id 
key 
value 
car_id 
date 

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

有没有办法用select命令做到这一点?

回答

2

示例数据:

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命令可能是这样的:

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