【问题标题】:SQL Update Concat ArraySQL 更新连接数组
【发布时间】:2018-10-27 21:50:26
【问题描述】:

我正在尝试将值添加到现有的整数列数组。我正在使用这个查询。

UPDATE users SET conversations_id = CONCAT(conversations_id, '{2,3}');

它给了我这个错误。

ERROR:  column "conversations_id" is of type integer[] but expression is of 
type text
LINE 1: UPDATE users SET conversations_id = CONCAT(conversations_id,...
                                        ^
HINT:  You will need to rewrite or cast the expression.
SQL state: 42804
Character: 37

我知道它告诉我第二个参数是文本,但它要求这样写,就像在这里一样。

UPDATE users SET conversations_id = '{1,2,3}';

我尝试将第二个参数转换为数组,但我不确定如何。我也尝试将其标记为 VARIADIC,但同样,在这种情况下我不知道该怎么做。

如何向该数组添加新值?

【问题讨论】:

    标签: sql arrays postgresql concat


    【解决方案1】:

    As documented in the manual 将数组附加到另​​一个数组的运算符是|| 或函数array_cat()concat()用于 text/varchar 值。

    所以你需要使用:

    UPDATE users 
       SET conversations_id = conversations_id || '{2,3}'::int[];
    

    UPDATE users 
       SET conversations_id = array_cat(conversations_id, '{2,3}'::int[]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-17
      • 2011-05-05
      • 2011-09-22
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多