2016-06-03 2 views
1

나는 세 개의 테이블을 하나씩 가지고 있습니다. 두 번째는 인출이고 세 번째는 지갑 밸런스 테이블에서 지갑 균형입니다. 저는 모든 대변 잔액과 차감 잔액을 더한 다음 대변 잔액을 빼야합니다. 실제 잔액에 대한 차변 잔액에서 모든 세 테이블의 데이터를 검색합니다. 내 테이블 구조가 아래에 있습니다.다른 테이블에서 데이터를 더하거나 빼는 방법

Userregistration 테이블

........................................ 
id  fullname mobile_no  email 
......................................... 
5  varun   12344567 [email protected] 
6  nitin   12345678 [email protected] 

내가이 출력하고자

............................... 
wid userid withdraw_status 
............................... 
1  5   pending 
2  6   pending 

walletbalance

.......................................... 
id user_id balance transaction_type 
.......................................... 
1  5  100  credit 
2  5  20   debit 
3  6  200  credit 
4  6  100  debit 

철회 :

................................................................. 
wid user_id balance withdraw_status fullname mobile_no email 
................................................................. 
1  5  80  pending  varun  12344567 [email protected] 
2  6  100  pending  nitin  12344567 [email protected] 

나는 사용자의 실제 균형을 위해 이것을 시도했지만 난 Thanku 팀이 MySQL의 지식에 대한 인식이

SELECT SUM(`balance`) as b1 from walletbalance WHERE `user_id`='5' and `transaction_type`='credit' UNION SELECT SUM(`balance`) as b2 from walletbalance WHERE `user_id`='5' and `transaction_type`='debit' 

답변

1
SELECT t2.wid, t2.userid AS user_id, t3.balance, t2.withdraw_status, 
    t1.fullname, t1.mobile_no, t1.email  
FROM Userregistration t1 
INNER JOIN withdraw t2 
    ON t1.id = t2.userid 
INNER JOIN 
(
    SELECT user_id, 
     SUM(CASE WHEN transaction_type = 'credit' 
       THEN balance 
       ELSE balance * -1 
      END) AS balance 
    FROM walletbalance 
    GROUP BY user_id 
) t3 
    ON t1.id = t3.user_id 
+0

ahieve 할 수 없습니다. –

관련 문제