Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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);
share|improve this question
1  
Yes, you need to use union between your selects. –  John Barça yesterday

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.