update b set b.户数  = aa.cnt
from b
join (select left(jsdm,2) as jsdm,count(*) as cnt from a group by left(jsdm,2)) aa
on aa.jsdm = b.fgdm

解决方案 »

  1.   

    update b set 户数=(select count(*) from a where left(jsdm,2)=b.fgdm)
      

  2.   

    或:update b set 户数=(select count(*) from a where jsdm like b.fgdm+'%')
      

  3.   

    update b  set b.户数  = (select cnt from
    (select left(jsdm,2) as fgdm,count(*) as cnt) tmp where tmp.fgdm=b.fgdm)
      

  4.   

    like 的速度可能比 left快一点。
      

  5.   

    update b
    set b.户数 =aa.hs
    from b inner join (select left(jsdm,2) dm,count(*) hs 
    from (select distinct jsdm from a) tem group by left(jsdm,2)) aa
    on aa.fgdm=b.fgdm
      

  6.   

    update b set b.户数 =isnull(aa.hs,0)
    from b left join (select left(jsdm,2) dm,count(*) hs 
    from (select distinct jsdm from a) tem group by left(jsdm,2)) aa
    on aa.fgdm=b.fgdm
      

  7.   

    update b set 户数  = (select count(*) from a group by left(jsdm,2) having left(jsdm,2)=b.fgdm) 
      

  8.   

    select distinct jsdm into #tmp from a
    update b set 户数=(select count(*) from #tmp where substring(jsdm,1,2)=b.b.fgdm)
    drop table #tmp
    go
      

  9.   

    update tableB set 户数 = B.户数 from 
    (select left(jsdm,2) fgdm, sum(1) 户数 from 
    (select distinct jsdm, (case when ysxz = 'sh' then 'sc' else ysxz end) ysxz from tableA) A) B
    where tableB.fgdm = B.fgdm
      

  10.   


    update b set 户数=(select sum(1) from a where jsdm like b.fgdm+'%')