[GIS] PostgreSQL: insert from multiple select

postgresqlsql

Is it possible in PostgreSQL to insert field values into a table using multiple selects from various other tables? Something like the following SQL (I have over simplified my original problem). I don't want to perform two individual insert operations.

insert into table_1 (name, id)
(select name, id from table_2 limit 1),
(select name, id from table_3 limit 1);

Best Answer

Use UNION or UNION ALL to build one result from your two selects:

insert into table_1 (name, id)

select name, id from table_2 limit 1 
UNION
select name, id from table_3 limit 1;

UNION will remove the duplicated rows from the union, UNION ALL will include all.

Related Question