表test、test2中均含有两个字段num number(10),ch varchar2(100),现在我想把test2表中的num与test表中相等的那些ch字段的值更新为test表中的ch字段值。在SqlServer中可以通过如下语句实现:
update test2 set test2.ch=test.ch where test2.num=test.num;
update test1 a1
set ch=(select ch from test2 b1 where a1.num=b1.num)
where num in ( select a2.num from test1 a2,test2 b2 where a2.num=b2.num);
总结一下:
通过一个表的某些字段更新另外一个表的相应字段,在SQLSERVER中可通过如下方法实现:
update 要更新的表 set 要更新的字段=源表中对应的字段 where 要更新表中的字段=源表中的字段;
在ORACLE中可通过如下方法实现:
update 要更新的表 set 要更新的字段=(从源表中选择出来的符合条件的要更新的值的集合) where 要
更新的字段 in (从要更新表中选出的要更新字段的所有值的集合);
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/35489/viewspace-667170/ ,如需转载,请注明出处,否则将追究法律责任。