2011-02-02 21 views
2

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

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手工来尝试自己。

1

是:

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