我想要这样的结果
来自这些表。
我什至不知道如何用 php 来做。我什至尝试按日期加入付款和发票表,但没有成功。
这是一个采购系统,此查询将按日期显示所有付款的摘要和发票总数。
我想到了一个解决方案,首先从发票中选择所有日期,然后从付款中选择所有日期并取其并集。然后检查该日期是否有发票,然后检查该日期是否有付款。但是这样查询会比较多。
最佳答案
select
x.date,
sum(x.invoiceTotal) as invoiceTotal,
sum(x.paymentsMade) as paymentMade
from
(select
i.date,
sum(i.rate * i.quantity /*?*/) as invoiceTotal,
null as paymentMade
from
invoice i
inner join invoiceitem ii on ii.invoiceId = i.invoiceId
group by
i.date
union all
select
p.date,
null as invoiceTotal,
sum(p.amount) as paymentMade
from
payment p
group by
p.date) x
group by
x.date
order by
x.date
关于sql - mysql 查询 : show summary of all payments made and total of invoices by date,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4228926/