3

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);
  • 1
    Yes, you need to use union between your selects. – John Powell May 25 '15 at 8:04
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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