假设有两张表A和B,A和B通过B.id关联,并且A中有b_name列,这一个列取B的name。
现在通过A和B的关联,更新A.b_name数据,条件为A.b_name为空,并且B.name不为空。
update A t1 left join B t2 on t1.b_id = t2.id
set t1.b_name = t2.name
where t1.b_name is null and t2.name is not null;
这种场景多发两种情况,一种是冗余字段,当一段时间后,两边不一致,需要手动同步。二种是新加一个冗余字段,需要根据关联id去取并做更新。有的时候做数据处理的时候,也会有这种操作。