sql通过主键关联,将表中某个字段值为空的地方修改为另一个表的对应值
表a有20万条数据,表b有2万条,表b是从表a中挑出的,并进行了一些字段值的添加或修改,两个表可通过主键id对应。现将表a里column1值为空的用表b中对应值进行替换。能否写出完整的语句?
update a set a.column1=(select column1 from b where a.id=b.id) where a.id is null
我是这样写的,可以吗,有什么问题没?(本人刚接触sql数据库不久)
update a,b set a.字段 = b.字段 where a.id=b.id and ifnull(a.字段,'') = ''
关联的时候用if去判断这个字段取有值的就好了
select
if(a.xx is null,b.xx,a.xx)
from a
left join b
on a.id = b.id