我正在尝试将table2中的值插入table1,其中addressB与addressA匹配。到目前为止我的尝试是下面的代码。查询似乎将新的空白记录插入到table1而不是我想要它做的事情。有人可以告诉我这个查询是否正确吗?
INSERT INTO database1.table1 (name)
select database2.table2.distName
from database2.table2
left join database1.table1
On database2.table2.addressB = database1.table1.addressA
where database2.table2.addressB = database1.table1.addressA
我的sql小提琴
http://sqlfiddle.com/#!9/aca3e/2
答案 0 :(得分:2)
INSERT INTO database1.table1 (name)
select database2.table2.a_distName2
from database2.table2
left join database1.table1
On database2.table2.addressB = database1.table1.addressA
where database1.table1.addressA is null;
答案 1 :(得分:1)
在sql小提琴之后,我更加确信你需要使用update
而不是插入。
update table1 inner join table2 on table1.addressA=table2.addressB
set table1.distname=table2.distname
插入后table1.id列中的空值是table1.id上缺少主键并且在同一列上缺少auto_increment规范的结果。
答案 2 :(得分:0)
INSERT INTO database1.table1 (name)
select database2.table2.a_distName2
from database2.table2
inner join database1.table1
On database2.table2.addressB = database1.table1.addressA