postgres:使用子查询设置数组的值?

Posted

技术标签:

【中文标题】postgres:使用子查询设置数组的值?【英文标题】:postgres: Setting the value of an array with a subquery? 【发布时间】:2011-02-02 20:25:06 【问题描述】:

在 postgres 中,您可以将 INSERT 中的数组值设置为子查询的结果吗?喜欢:

INSERT INTO mytable
VALUES( SELECT list_of_integers FROM someothertable WHERE somekey = somevalue);

mytable 的一列只有 integer[] 类型,而另一列 list_of_integers 也是 integer[] 类型?

【问题讨论】:

【参考方案1】:

您需要unnest 函数。我想你会像这样使用它:

INSERT INTO mytable
SELECT set_of_integers
FROM unnest(
  SELECT list_of_integers
  FROM someothertable
  WHERE somekey = somevalue
) t(set_of_integers)

但我没有 PostgreSQL 可以亲自试用。

【讨论】:

【参考方案2】:

是的:

INSERT INTO
     mytable
    (column1, column2, an_array_of_integers_column)
VALUES
    (2, 'bbb', (SELECT list_of_integers FROM someothertable WHERE somekey = somevalue));

【讨论】:

以上是关于postgres:使用子查询设置数组的值?的主要内容,如果未能解决你的问题,请参考以下文章