mysql 自A表复制B表字段,条件是B表的aid字段是A的id字段

A表内字段 

id    name     age    sex

11    NULL  NULL  NULL 

22    NULL  NULL  NULL

33    NULL  NULL  NULL

B表内字段

id  name    age    sex    aid

6   MR.I     22   MALE   11

12 MR.L    25   MALE   22

33 MR.U   46   MALE   33

B.aid保存的是A的id

希望根据B.aid把B.name age sex 数据update或者insert into 到相对应(B.aid)的A表 name age sex 里

update a t
inner join b t2 on t2.aid = t.id
set t.name = t2.name, t.age = t2.age, t.sex = t2.sex

 

简单粗暴点:update test1 t1 set name=(select t2.name from test2 t2 where t1.id =t2.aid)