create table t1
(
a varchar(10),
b varchar(10),
c varchar(10)
)create table t2
(
a varchar(10),
c varchar(10),) insert into t1 values('赵','asds',null)insert into t1 values('钱','asds','100')insert into t1 values('孙','asds','80')insert into t1 values('李','asds','70')
insert into t2 values('赵','')
insert into t2 values('钱','10')
这是我的语句
update t1 set c  = (select  C from t2 where t1.a=t2.a ) 单我这样更新时。我的本意是吧钱的c更新为10,可是结果打出我以外,t1表中除了钱和赵以外,其他木有对应的孙,李。他们的值都为NULL
这是为何
批量更新

解决方案 »

  1.   

    这样写update t1   set t1.c  = t2.c from t2 where t1.a=t2.a
      

  2.   

    update t1 set c=t2.c from t2 where t1.a=t2.a  试试这个
      

  3.   

    update t1   set t1.c  = t2.c from t2 where t1.a=t2.a and t1.c<>t2.c
      

  4.   

    之所以这样是由于,你用的是子查询,比如当t1表的a字段是'孙',那么就去t2表的a字段中去查询,发现t2表的a字段中没有'孙'的数据,
    ,也就是找不到,于是乎就返回了null,也就是:update t1 set c  = null
    那么这样就把t1表中的a字段的值为'孙'的记录,中的c替换为null了。应该改成这样,就对了,只会更新都有的记录:
    update t1 set c=t2.c from t2 where t1.a=t2.a