当前位置: 代码迷 >> Sql Server >> 同一个表中补上已经存在的数据解决办法
  详细解决方案

同一个表中补上已经存在的数据解决办法

热度:66   发布时间:2016-04-24 08:51:43.0
同一个表中补上已经存在的数据

cityid与postcode对应,我想把postcode为空的都补上,postcode为空的对应的值在这个表里都有,请问为空的怎么补上
------解决思路----------------------
update tb set postcode=b.postcode from table1 tb left join (select distinct cityid,postcode from table1 where postcode<>'') b on tb.cityid=b.cityid
where tb.postcode=''

试试这个
------解决思路----------------------
with t as (
select distinct cityid,postcode
from 原表名
where isnull(postcode,'')<>''
)
update 原表名 set postcode=t.postcode
from 原表名 join t on t.cityid=原表名.cityid
where isnull(原表名.postcode,'')=''
  相关解决方案