我正在制作一个小发票应用程序而且我一直在查询以获取我的发票的总开放金额。
一点解释:
我在数据库中有2个表invoices
和payments
,这些表与列invoice_id
相关。一张发票可能有多笔付款。
我想得的是所有发票的总开仓金额。
这是我尝试的但它不起作用。
total_paid金额不正确,它是最后一张发票的总和,而不是所有发票的总和。
SELECT
COUNT( * ) AS count,
SUM( invoice_amount_total ) AS total,
(SELECT
SUM( amount ) AS total
FROM `invoice_payments`
WHERE `invoice_id` = s.id) AS total_paid
FROM sales_invoices AS s
WHERE s.administration_id = 3
AND (`invoice_amount_total` <> (SELECT
SUM( `amount` ) AS sum
FROM `invoice_payments`
WHERE `invoice_id` = s.id)
OR (SELECT
SUM( amount ) AS sum
FROM `invoice_payments`
WHERE `invoice_id` = s.id)IS NULL)
有什么想法吗?