5

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
  • 1
    Yes, you need to use union between your selects. May 25, 2015 at 8:04

1 Answer 1

6

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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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