update tab_1 a set value1 = (select value1 from tab_2 b where a.bm=b.bm) 这条SQL语句不管它该不该更新所有记录,但实际上它更新了 TAB_1这张表中的每一条记录,更糟糕的是TAB_2表中的 bm 字段的内容即使没有出现在TAB_1表中,这条SQL语句也将会把 TAB_1相对应的 value1字段置为null,这是绝对不准许的,因此要进行更有选择性的更新以避免错误的发生。
有两种方法可以解决这个问题:
Method 1:
update twtlib.tab_1 a
set value1=(select value1 from twtlib.tab_2 b where a.bm=b.bm)
where bm in (select bm from twtlib.tab_2);
Method 2:
update twtlib.tab_1 a set value1 = (select value1 from twtlib.tab_2 b where a.bm=b.bm)
where exists (select 1 from twtlib.tab_2 b where a.bm=b.bm);
然后查看tab_1的内容,没有被误改为null的记录了,符合要求。
仅供参考。