Sql 在更新oracle中使用选择结果

Sql 在更新oracle中使用选择结果,sql,oracle,Sql,Oracle,我搜索并发现了许多与我类似的问题,但没有一个解决方案适合我。这是在Oracle中,我不知道版本(我只通过SQL Developer访问它)。我有两个表(省略了不相关的列): 我有一个疑问: select user_id, promotion_id, sum(amount) from t_points_receipt where promotion_id = 10340 group by user_id, promotion_id; 我想做的是使用sum(amount)从结果中更新t_user

我搜索并发现了许多与我类似的问题,但没有一个解决方案适合我。这是在Oracle中,我不知道版本(我只通过SQL Developer访问它)。我有两个表(省略了不相关的列):

我有一个疑问:

select user_id, promotion_id, sum(amount) from t_points_receipt
 where promotion_id = 10340 group by user_id, promotion_id;

我想做的是使用sum(amount)从结果中更新t_user_promotion_points。其中t_user_promotion_points.user_id=(results)。user_id和t_user_promotion_points.promotion_id=(results)。promotion_id。有什么方法可以做到这一点吗?

您可以为要设置的值创建相关子查询:

update t_user_promotion_points p
set total_points = select sum(amount) from t_points_receipt r
on p.user_id = r.user_id 
where r.promotion_id = 10340 and
where t.promotion_id = 10340
update t_user_promotion_points p
set total_points = (select sum(amount) from t_points_receipt r where p.user_id = r.user_id and p.promotion_id = r.promotion_id)
where p.promotion_id = 10340;

感谢您的快速回复!但是,我得到了这个错误:“从命令的第1行开始的错误:update t\u user\u promotion\u points p set total\u points=select sum(amount)从p.user\u id=r.user\u id上的t\u points\u receipt r,其中r.promotion\u id=10340,其中t.promotion\u id=10340命令行错误:2列:19错误报告:SQL错误:ORA-00936:缺少表达式00936。00000-“缺少表达式”*原因:*操作:'对于上面的格式设置,很抱歉,我尝试修复它,但它在我身上超时。
update t_user_promotion_points p
set total_points = (select sum(amount) from t_points_receipt r where p.user_id = r.user_id and p.promotion_id = r.promotion_id)
where p.promotion_id = 10340;