我有三个表App
,Loan
和Payment
。
我使用以下方法从三个表中选择了所有数据:
SELECT applicant.*, loan.*, payment.*
FROM (applicant INNER JOIN loan ON applicant.app_trn = loan.app_trn)
INNER JOIN payment ON loan.loan_reff_id= payment.loan_reff_id ORDER by app_fname
在表格的最底部,我想显示贷款所欠的总金额。
有人可以帮助我,表格如下:
App
:app_id
,name
,address
; Loan
:app_id
,loan_id
,欠款; Payment
:loan_id
,付款金额。我希望能够显示表格底部的欠款总额。
答案 0 :(得分:0)
(
SELECT applicant.*, loan.*, payment.*
FROM (applicant INNER JOIN loan ON applicant.app_trn = loan.app_trn)
INNER JOIN payment ON loan.loan_reff_id= payment.loan_reff_id ORDER by app_fname
)
union
(
SELECT
0 as app_id,
0 as name,
0 as address,
0 as loan_id,
sum(loan amount owed),
0 as payment amount
FROM (
SELECT applicant.*, loan.*, payment.*
FROM (applicant INNER JOIN loan ON applicant.app_trn = loan.app_trn)
INNER JOIN payment ON loan.loan_reff_id= payment.loan_reff_id ORDER by app_fname
) t
)
这个想法是将联合对与聚合函数总和结合使用。