'Insert value of one column from one table to another table based on where condition

I have one question . Suppose there is one table rules in which column department, action ,left_source and right_source,left_source_id,right_source_id is there .Another table is source table where column is name,I'd . Now i have to insert rules to rule table but in left_source_id and right_source_id i have to insert value from source table based on I'd column . I need some immediate help . (Source table column I'd contains all the name of left_source and right_source )



Solution 1:[1]

Insert Select...union all..select for example

drop table if exists t,t1;

create table t(id int,leftsource varchar(1),rightsource varchar(1));
create table t1(id int,val varchar(1));

insert into t1 values
(1,'l'),(2,'r');

insert into t
select id,val,null from t1 where id = 1
union all
select id,null,val from t1 where id = 2

select * from t;

+------+------------+-------------+
| id   | leftsource | rightsource |
+------+------------+-------------+
|    1 | l          | NULL        |
|    2 | NULL       | r           |
+------+------------+-------------+
2 rows in set (0.001 sec)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 P.Salmon