我已经安排付款,所以我有这些表格:
customer
+id
paymentSchedule
+id
+customer_id
+amount //total price
+dueDate //date to be paid
payments
+id
+date
+customer_id
+paymentSchedule_id
+amount //amount paid, it can be a partial payment
如何编写查询来获取客户今天的到期金额。 我的意思是我需要加入表格(这是我的主要问题),然后减去 payment.mount 的总和减去预定Paymens.amount 的总和 但是..怎么办? 提前致谢
最佳答案
这可能不是 100%,但应该非常可靠,可以帮助您进行调整:
SELECT customer_id, (due.amount - paid.amount) as amountDue
FROM
(SELECT customer_id, SUM(amount) as amount
FROM paymentSchedule
WHERE dateDate <= getDate()
and customer_id = @custid) as due
LEFT JOIN
(SELECT customer_id, SUM(amount) as amount
FROM payments
WHERE customer_id = @custid) as paid ON paid.customer_id = due.customer_id
关于mysql - 如何编写查询来计算今天的到期金额?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9245912/