根据where条件将一列的值从一个表插入到另一个表



我有一个问题。假设有一个表规则,其中有department、action、left_source和right_source、left_source_id、right_ssource_id列。另一个表是source表,其中column是name。现在我必须将规则插入到规则表中,但在left_source_id和right_ssource_id中,我必须根据i'd列插入源表中的值。我需要一些即时的帮助。(源表列我会包含left_Source和right_Source的所有名称(

插入选择。。。联合所有。。例如选择

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)

最新更新