简体   繁体   English

在更新oracle中使用select结果

[英]use select results in update oracle

I have searched and found many questions similar to mine but none of the solutions work for me. 我搜索过并发现了许多与我类似的问题,但没有一个解决方案适合我。 This is in Oracle, I don't know the version (I only access it through SQL Developer). 这是在Oracle中,我不知道该版本(我只通过SQL Developer访问它)。 I have two tables (irrelevant columns omitted): 我有两个表(省略了不相关的列):

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) 

And, I have this query: 而且,我有这个问题:

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

What I would like to do is update t_user_promotion_points with sum(amount) from the results where t_user_promotion_points.user_id = (results).user_id and t_user_promotion_points.promotion_id = (results).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。 Is there a way to do this? 有没有办法做到这一点?

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

You can create a correlated subquery for the value to set: 您可以为要设置的值创建相关子查询:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM