我正在制作一个小发票申请,但我一直在查询以获取我的发票的总未结金额。
一点解释:
我在数据库中有 2 个表,invoices
它们payments
与 column 相关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)
有任何想法吗?