读书人

请教怎么用update实现如下功能

发布时间: 2012-02-13 17:20:26 作者: rapoo

请问如何用update实现如下功能
现有a,b两个表
a表结构如下
id 单价 数量
1 2.3 3
2 2.5 9
3 6.5 20
......

b表结构如下
id 总费用
1 ?

现在如何使用sql的update功能 把a表的价格和数量的积通过id分别对应到b表的总费用上?


急!等求答案

[解决办法]
是指A的id有多?
update B
set 总费用=T.费用
from B,
(select id,sum(a.amount*a.price) as 费用
from A group by id) T
where B.id=T.id

[解决办法]
update b
set 总费用 = c.总费用
from b,(select id , sum(amount*price) 总费用 from a group by id) c
where b.id = c.id

读书人网 >SQL Server

热点推荐