我搜索过并发现了许多与我类似的问题,但没有一个解决方案适合我。这是在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。有没有办法做到这一点?
答案 0 :(得分: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
答案 1 :(得分: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;