当需要做令人恶心的数据修复的任务事情时;
常常需要使用以下语句,先把原始数据表备份出来。
create table 表名_bak as (select * from 表名);
但当你的修复方案失败的时候,往往需要先把数据先还原回去。
平时使用两张表关联查询较多,更新动作较少。
以下语句个人觉得比较好用,并且逻辑清晰,增加字段也不用重写。
Mysql:
update 表名1 a join 表名2 b on a.xx=b.xx set a.xx1=b.xx1,a.xx2=b.xx2;
Sql server:
update 表名1
set 表名1.xx=表名2.xx
from 表名1 left join 表名2
on 表名1.关联字段1=表名2.关联字段2;
Oracle:
update 表名1
set 表名1.xx=(select 表名2.xx from 表名2 where 表名1.关联字段1=表名2.关联字段2)
where exists (select 1 from 表名2 where 表名1.关联字段1=表名2.关联字段2);