如何用sql语句,将表2中与表1中含有相同字符行,中的数据插入表1中

表1
a b c
yy 4
k 9
xx 7
表2
a c
k 65
jx 8
xx 5
最后的结果是这样的

表1
a b c
yy 4
k 9 65
xx 7 5

直接插入表1中

建表插入数据:

create table table1
(a varchar(10),
b int,
c int);

create table table2
(a varchar(10),
c int);

insert into table1 values ('yy',4,null);
insert into table1 values ('k',9,null);
insert into table1 values ('xx',7,null);

insert into table2 values ('k',65);
insert into table2 values ('jx',8);
insert into table2 values ('xx',5);

mysql下执行:

update table1 a,table2 b set a.c=b.c where a.a=b.a

mysql下执行后结果:

sqlserver下执行:

update table1 set c=b.c from table1 a inner join table2 b on a.a=b.a

sqlserver下执行后结果:

以后提问时说明用的什么数据库,不同数据库语法都不同的。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-11-14
表结构 能列出来吗?
相似回答