日期:2014-05-18  浏览次数:20612 次

请问如何用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