我有个问题。我有 4 张桌子:
Invoice_Payment、发票、客户和日历
基本上,我有以下查询,它运行良好,除了没有 date_due 的月份不返回。IE 只会返回带有 date_due 的月份。
注意:日历表仅列出一年中的每一天。它包含一个 col 调用 date_field
数据库: http: //oberto.co.nz/jInvoice.txt
预期输出:我当前的查询如下,将返回如下内容:
month total
August 5
September 0
October 196
November 205
December 214
January 229
请注意九月如何没有返回?这是因为表 Invoice_Payment 没有 date_due 记录
我想我必须使用左联接并加入日历表,例如: LEFT JOIN Calendar ON Invoice_Payments.date_paid = Calendar.date_field。但我没有运气
SELECT MONTHNAME(Invoice_Payments.date_paid) as month, SUM(Invoice_Payments.paid_amount) AS total
FROM Invoice, Client, Invoice_Payments
WHERE Client.registered_id = 1
AND Client.id = Invoice.client_id
And Invoice.id = Invoice_Payments.invoice_id
AND date_paid IS NOT NULL
GROUP BY YEAR(Invoice_Payments.date_paid), MONTH(Invoice_Payments.date_paid)
任何帮助表示赞赏。