update bproduct set pnumber = B.sum_num from bproduct A inner join (select in_yhid,sum(in_num) as sum_num from yh_list group by in_yhid) B on A.ProdID=B.in_yhid
update bproduct set pnumber=(select sum(in_num) from yh_list where ProdID=b.in_yhid ) from bproduct b
update bproduct set pnumber = B.sum_num from bproduct A inner join (select in_yhid,sum(in_num) as sum_num from yh_list group by in_yhid) B on A.ProdID=B.in_yhid
if object_id('bproduct') is not null drop table bproduct go create table bproduct (ProdID int, pnumber int) insert bproduct select 4501, 0 union all select 4502, 0if object_id('yh_list') is not null drop table yh_list go create table yh_list(in_yhid int , in_num int) insert yh_list select 4501, 17 union all select 4501, 10 union all select 4502, 11 union all select 4502, 12 go update bproduct set pnumber=pnumber+num from ( select in_yhid, num=sum(in_num) from yh_list group by in_yhid )t where t.in_yhid=bproduct.prodidselect * from bproduct
pnumber = B.sum_num
from bproduct A inner join
(select in_yhid,sum(in_num) as sum_num from yh_list group by in_yhid) B on A.ProdID=B.in_yhid
from bproduct b
pnumber = B.sum_num
from bproduct A inner join
(select in_yhid,sum(in_num) as sum_num from yh_list group by in_yhid) B on A.ProdID=B.in_yhid
drop table bproduct
go
create table bproduct (ProdID int, pnumber int)
insert bproduct select 4501, 0
union all select 4502, 0if object_id('yh_list') is not null
drop table yh_list
go
create table yh_list(in_yhid int , in_num int)
insert yh_list select 4501, 17
union all select 4501, 10
union all select 4502, 11
union all select 4502, 12
go
update bproduct
set pnumber=pnumber+num
from (
select in_yhid, num=sum(in_num)
from yh_list
group by in_yhid
)t
where t.in_yhid=bproduct.prodidselect * from bproduct