0
SELECT에서 각 행에 대한 추가 쿼리 (UPDATE)를 실행하는 방법은 무엇입니까? select에서 각 행의 금액을 가져와 사용자의 잔액 테이블로 보내야합니다.SELECT 쿼리의 각 행에 대한 연산
예 :
status 0 - open
status 1 - processed
status 2 - closed
내 선택 문 : 시장 테이블
id;user_id;amount;status
4;1;1.00000000;0
6;2;2.60000000;0
5;3;2.00000000;0
7;4;4.00000000;0
에서
select id, user_id, sell_amount, sell_currency_id
from (select id, user_id, sell_amount, sell_currency_id,
sum(sell_amount)
over (order by buy_amount/sell_amount ASC, date_add ASC) as cumsell
from market t
where (status = 0 or status = 1) and type = 0
) t
where 0 <= cumsell and 7 > cumsell - sell_amount;
선택 결과 우리는 (7) 양을 얻을 수 및 사용자 밸런스 테이블에 보낼 수 있습니다.
id;user_id;amount;status
4;1;0.00000000;2 -- took 1, sum 1, status changed to 2
6;2;0.00000000;2 -- took 2.6, sum=3.6, status changed to 2
5;3;0.00000000;2 -- took 2, sum 5.6, status changed to 2
7;4;2.60000000;1 -- took 1.4, sum 7.0, status changed to 1 (because there left 2.6 to close)
사용자의 균형 테이블
user_id;balance
5;7 -- added 7 from previous operation
포스트 그레스 버전 9.3