日期:2014-05-17 浏览次数:20430 次
update a set a.price=(select sum(price) from table b
where a.id=b.id)
from table a
where a.groupid=1
create table tb
(
GroupID int,
ID int,
Price int
)
insert into tb values(1,100,1000)
insert into tb values(2,100,2000)
insert into tb values(1,101,500)
insert into tb values(2,101,500)
update a set a.price = b.price
from tb a, (
select id,sum(price)price from tb
where groupid in(1,2)
group by id) b
where a.groupid = 1
select * from tb
/*
GroupID ID Price
1 100 3000
2 100 2000
1 101 1000
2 101 500
(4 行受影响)
declare @t table(id int ,groupid int, n int)
insert @t select 1, 100 , 1000
union all select 2 , 100 , 2000
union all select 1 , 101 , 500
union all select 2 , 101, 500
update a set n = N + (select SUM(n) m from @t where groupid=a.groupid and id=2)
from @t a
where a.id=1
select * from @t