2013-07-10 4 views
0

이 문제를 해결하려면 함수 또는 트리거가 필요합니까 ??다른 테이블의 같은 열이 변경된 경우 열을 업데이트하는 방법 (postgresql)

customer_details :::

custid name creditid 
---------------------------- 
    2  a  1 
    3  b  2 
    4  c  3 

BALANCE_AMOUNT :::

creditid credit_type balance 
----------------------------------- 
    1   rent  1000 
    1   transport 2000 
    1   food  1000 
    1   fruits  1500 
    2   rent  1500 
    2   transport 1020 
    2   food  1200 
    2   fruits  1000 
    3   transport 1600 
    3   rent  2000 
    3   food  1540 
    3   fruits  1560 

Pay_the_loan :::

creditid credit_type  Pay  status 
--------------------------------------------- 
    1   rent   500  null 
    2   fruits   600  null 
    3   transport  400  null 
    1   fruits   500  null 

나는 특정 ok에 대한 pay_the_loan table에서 status 열을 업데이트되면 creditid i 전화부 ..

then 그것이 BALANCE_AMOUNT 테이블에 balance 열의 양을 deduct한다 (갱신 pay_the_loan 세트 상태 = creditid = 2 여기서 '확인')과이 업데이트되어야 즉 . (BALANCE_AMOUNT 테이블 1000-600 = 400where balance_amount.credit_type = 과일과 균형 양 테이블에서 creditid = 2)

가능한 포스트 나에게 Function 또는 Trigger이 프로를 해결하기 위해 흠?

+0

'1)'업데이트 조건은 언제 정의해야합니까? '2)'는 실제 update 문을 정의합니다 : 무엇을 변경해야하며, 어떻게해야합니까? '3)''2'를 함수로,'1'을 트리거로 변환합니다. '4)'이익!. '0)'테이블에 PGs/FKs가 필요합니다. – wildplasser

+0

좋아, 이걸 더 잘 이해할 수있는 이런 예를 들고 갈거야 !! 문제는 @ wildplasser – 09Q71AO534

답변

0

원래의 대출로 loan_amount 테이블을 만들고 약간의 구조 조정을하는 것이 더 좋을 것입니다. balance_amount은 모든 지불액이 차감 된 현재 잔액을 보여주는보기로 만 설정하십시오. 이렇게하면 지불 금액을 정정해도 시스템에 잘못된 균형이 표시되지 않습니다.

현재 잔액을 계산하는보기는 다음과 같을 수 있습니다.

CREATE VIEW balance_amount AS 
    SELECT la.creditid, la.credit_type, amount 
       - COALESCE(SUM(pay),0) balance 
    FROM loan_amount la 
    LEFT JOIN pay_the_loan pl 
     ON la.creditid = pl.creditid 
    AND la.credit_type = pl.credit_type 
    AND pl.status = 'OK' 
    GROUP BY la.creditid, la.credit_type, la.amount; 

An SQLfiddle with the whole setup.

+0

http://sqlfiddle.com/#!12/7846f/1 여기에 같은 논리가 작동하지 않을 수 있습니다 ... @ joachim Isaksson – 09Q71AO534

+0

@ user2561626 죄송합니다, 'IFNULL'에 대한'COALESCE'해야합니다 PostgreSQL. http://sqlfiddle.com/#!12/7846f/4 –

+0

더 자세히 설명 할 수 있습니다 !! http://sqlfiddle.com/#!12/61942/1 – 09Q71AO534

0

동일한 쿼리에 두 테이블을 업데이트 할 수 있습니다

update 
    pay_the_loan 
set 
    pay_the_loan.status='ok', 
    balance_amount.balance=balance_amount.balance-pay_the_loan.Pay 
from balance_amount 
where 
    pay_the_loan.creditid=balance_amount.creditid 
    and 
    pay_the_loan.creditid=2 
    and 
    balance_amount.credit_type='fruits'; 

업데이트. postgresql update 성명 중 the documentation을 읽었습니다. 분명히 틀렸고 단일 쿼리에서 두 테이블을 업데이트 할 수 없습니다. 그러나, 나는 여전히 당신이 여기서 방아쇠가 필요 없다고 생각합니다. 두 개의 업데이트 쿼리를 하나씩 사용하십시오.

update 
    pay_the_loan 
set 
    status='ok' 
where 
    pay_the_loan.creditid=2; 

update 
    balance_amount 
set 
    amount=balance_amount.amount-pay_the_loan.Pay 
from pay_the_loan 
where 
    pay_the_loan.creditid=balance_amount.creditid 
    and 
    pay_the_loan.creditid=2 
    and 
    balance_amount.credit_type='fruits'; 
+0

이것은 SQL FIDDLE sqlfiddle.com/#!12/3e6a7/18 해결하십시오 이 쿼리 ... @mikhail makarov – 09Q71AO534

+0

내 접근 방식을 시도 했습니까? 기능이나 트리거가 필요 없습니다. 상태를 '확인'으로 설정 한 동일한 쿼리에서 잔액을 업데이트 할 수 있습니다. 내가 제안한 것을 이해하니? –

+0

http://sqlfiddle.com/#!12/35a5e/2 오류가 거의 없음 – 09Q71AO534

관련 문제