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));
精彩评论