使用mysql在html表的底部显示总和

时间:2012-03-13 02:24:23

标签: mysql sql

我有三个表AppLoanPayment

我使用以下方法从三个表中选择了所有数据:

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

在表格的最底部,我想显示贷款所欠的总金额。

有人可以帮助我,表格如下:

  • Appapp_idnameaddress;
  • Loanapp_idloan_id,欠款;
  • Paymentloan_id,付款金额。

我希望能够显示表格底部的欠款总额。

1 个答案:

答案 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
)

这个想法是将联合对与聚合函​​数总和结合使用。