【问题标题】:Insert columns value into hstore data type in postgres将列值插入到 postgres 中的 hstore 数据类型中
【发布时间】:2020-02-15 16:17:38
【问题描述】:

我在 postgres 中有一个表,其中包含 id_0、turnr 和标签这三列。标签列数据类型为 hstore。 目前我正在使用这个不起作用的查询

INSERT INTO relation_15_02_2020 (tags)
VALUES
   (
   '
       "type"=>"restriction",
       "restriction"=>"(select distinct(turnr) from relation_15_02_2020  ) "
       '
   );

如何添加

"type"=>"restriction",
"restriction"=>" turnr value for respective id  

id_0 =1 标签的期望输出

 {"type"=>"restriction","restriction"=>"NoRightTurn"}

【问题讨论】:

    标签: postgresql hstore


    【解决方案1】:

    您想要更新行,而不是插入新行:

    update relation_15_02_2020 
      set tags = hstore(array['type', 'restriction'], array['restriction', turnr])
    where id_0 = 1;
    

    或者

    update relation_15_02_2020 
      set tags = hstore('type', 'restriction')||hstore('restriction', turnr)
    where id_0 = 1;
    

    【讨论】:

    • @poonampatel:如果您不想覆盖现有值,而是附加新键,请使用 set tags = tags || ....
    猜你喜欢
    • 2023-04-07
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    • 2014-08-07
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多