这是我当前的查询
$ sql =" SELECT full_name
,amount
,
SUM(payments
。payment_amount
)为totalPayment
,
borrowers
。amount
- SUM(payments
。payment_amount
)为totalBalance
,
date_borrowed
,borrowers
。id
来自borrowers
LEFT JOIN payments
点击borrowers
。id
= payments
。borrower_id
GROUP BY borrowers
。id
ORDER BY date_borrowed
DESC";
答案 0 :(得分:1)
联接将为您提供所有付款记录,您只需要最后一个。做一个子查询:
$sql = "SELECT
full_name, amount, SUM(payments.payment_amount) as totalPayment,
borrowers.amount - SUM(payments.payment_amount) as totalBalance,
date_borrowed, borrowers.id,
(SELECT payment_date FROM payments
WHERE borrowers.id=payments.borrower_id
ORDER BY payment_date DESC LIMIT 0,1)
AS last_payment
FROM borrowers GROUP BY borrowers.id ORDER BY date_borrowed DESC";