![](/img/trans.png)
[英]PSQL query to insert the records from one table to another based on condition
[英]Insert value of one column from one table to another table based on where condition
我有一个问题。 假设有一个表规则,其中列部门、操作、left_source 和 right_source、left_source_id、right_source_id 存在。另一个表是源表,其中列是名称,我会的。 现在我必须将规则插入规则表,但在 left_source_id 和 right_source_id 我必须根据 Id 列从源表中插入值。 我需要一些即时帮助。 (源表列包含 left_source 和 right_source 的所有名称)
例如插入 Select...联合所有..select
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.