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
这是为何
批量更新
(
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
这是为何
批量更新
,也就是找不到,于是乎就返回了null,也就是:update t1 set c = null
那么这样就把t1表中的a字段的值为'孙'的记录,中的c替换为null了。应该改成这样,就对了,只会更新都有的记录:
update t1 set c=t2.c from t2 where t1.a=t2.a