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

时间:2011-02-02 20:25:07

标签: postgresql

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

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

mytable只有一列integer[]而其他列list_of_integers也是integer[]类型?

2 个答案:

答案 0 :(得分: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亲自尝试一下。

答案 1 :(得分:1)

是:

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