34,590
社区成员
发帖
与我相关
我的任务
分享
declare @a table (id int,hits int)
insert into @a select 1,0
insert into @a select 2,0
insert into @a select 3,0
declare @b table (id int,hits int)
insert into @b select 1,3
insert into @b select 3,5
declare @c table (id int,hits int)
insert into @c select 1,10
insert into @c select 2,25
update @a set hits=
(select isnull(sum(hits),0) from @b where id=a.id)+
(select isnull(sum(hits),0) from @c where id=a.id)
from @a a
select * from @a
update a
set hits = (select sum(hits) from b表 where id = a.id) + (select sum(hits) from c表 where id = a.id)
from a表 a
create table #a (id int,hits int)
create table #b (id int,hits int)
create table #c (id int,hits int)
insert into #a values(1,0)
insert into #a values(2,0)
insert into #a values(3,0)
insert into #b values(1,1)
insert into #b values(3,5)
insert into #c values(1,10)
insert into #c values(2,25)
update #a set hits=a.hits from #a inner join (select id,hits=sum(hits) from (select * from #b union all select * from #c) b group by b.id) a on #a.id=a.id
select * from #a
id hits
----------- -----------
1 11
2 25
3 5
(3 行受影响)
update a
set hits = (select sum(hits) from b表 where id = a.id) +
(select sum(hits) from c表 where id = a.id)
from a表 a