1

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

describe t_points_receipt
Name              Null     Type          
----------------- -------- ------------- 
USER_ID           NOT NULL NUMBER(9)     
PROMOTION_ID      NOT NULL NUMBER(9)     
AMOUNT            NOT NULL NUMBER(9,2)   

describe t_user_promotion_points
Name         Null     Type        
------------ -------- ----------- 
USER_ID      NOT NULL NUMBER(9)   
PROMOTION_ID NOT NULL NUMBER(9)   
TOTAL_POINTS          NUMBER(9,2) 

而且,我有这个查询:

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

我想做的是用 t_user_promotion_points.user_id = (results).user_id 和 t_user_promotion_points.promotion_id = (results).promotion_id 的结果中的 sum(amount) 更新 t_user_promotion_points。有没有办法做到这一点?

4

2 回答 2

2
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
于 2012-10-19T23:35:49.537 回答
1

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

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;
于 2012-10-19T23:48:59.970 回答