【问题标题】:Append integer number to integer array in postgres在postgres中将整数附加到整数数组
【发布时间】:2021-09-30 04:42:43
【问题描述】:

我正在尝试向我现有的 postgres 表中的整数数组添加一个新的整数值。我在这里使用 array_append 方法,但出现“无法将类型 jsonb 转换为整数 []”错误。我的更新查询如下。

注意-我只是将它用于学习目的,所以我故意将电话号码保存为整数。

update querytesting set jsondoc=jsondoc||jsonb_build_object(jsondoc->'PhoneNumber',array_append(((jsondoc->'PhoneNumber')::int[]),'6789')) where id=1;

【问题讨论】:

  • 样本数据最好显示为formatted text。请参阅here,了解有关如何创建漂亮表格的一些提示。

标签: postgresql


【解决方案1】:

您必须提取数组,附加新数字,使用新数组创建一个新 JSON 并存储它。如果您将数据存储在规范化数据模型而不是 JSON 中,这是一个乏味且效率低下的过程。

UPDATE querytesting
SET jsondoc = jsonb_set(
                 jsondoc,
                 '{PhoneNumber}',
                 jsondoc -> 'PhoneNumber' || JSONB '[6789]'
              );
WHERE id=1;

【讨论】:

    猜你喜欢
    • 2013-01-04
    • 2013-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    相关资源
    最近更新 更多