数据库两张表中的数据叠加

来源:百度知道 编辑:UC知道 时间:2024/06/30 04:43:29
一张表是用户名和积分
如 jifen(useid,coin)
另一张表是用户名,商品,金额,消费日期
xiaofei(yonghu,goods,total,riqi)
------------------
要实现把2008-2-10购买几个商品的用户的total加到用户coin上。
--------------------------
update jifen
set coin=jifen+sum(b.total)
from jifen a,xiaofei b
where a.useid=b.yonghu
and b.riqi='2008-2-10'
------------------
出错:聚合不应出现在 UPDATE 语句的集合列表中
我要的是各个用户的消费金额加在各个用户的积分下,ytbelwxg 的答案都加在一起了,所有用户积分一样了。
---------------------------------
update jifen set coin=(select sum(total)
from xiaofei
where yonghu=jifen.userid and riqi='2008-2-10'
group by userid)
子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。

update a set a.coin=b.total from jifen a inner join (select yonghu,sum(total) as total from xiaofei where riqi='2008-2-10' group by yonghu) b on a.userid=b.yonghu

以上,希望对你有所帮助!

update jifen set coin=coin + (select sum(total) from xiaofei where yonghu=jifen.userid and riqi='2008-2-10')