开发者

postgres: Setting the value of an array with a subquery?

In postgres, can you se开发者_JAVA百科t the value of an array in an INSERT to the result of a subquery? Like:

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

Where that mytable just has as its one column a type of integer[] and that other column list_of_integers is also type integer[] ?


You want the unnest function. I think you'd use it like:

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

But i don't have PostgreSQL to hand to try it out myself.


Yes:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜