I am wondering if I can insert the result of a select query from
one table into another table. I have 3 tables: emp1, emp2, emp1_emp2, I want to insert pks of emp1 and emp2 into their association table emp1_emp2. If I can insert a row into emp1 and emp2 first, then insert their pks into their association table, I can save a second hit to db. Can I achieve something similar to this (I know the following won't work)?:
insert into emp1_emp2 values (select emp1_id from emp1, select from emp2_id from emp2);
Please ignore emp1_emp2 as an association table, its irrelevant here. Lets say I just want to insert pk of emp1 and pk of emp2, is there any way to do that?
Originally Posted by slimdave
how do you define the association between the two tables? What makes a value of emp1_id match a particular value of emp2_id?
But the following will only insert col1 of t2 and col1 of t3 both into
one column of t1. If t1 has two columns: col1 and col2, I want to insert col1 of t2 into col1 of t1, and insert col1 of t3 into col2 of t2,
how can we do that?
Originally Posted by slimdave
Well you could ...
Code:
insert into t1
select col1 from t2;
insert into t1
select col1 from t2
union all
select col1 from t3;
Bookmarks