SELECT
moneyIn.id,
moneyIn.TransactionNo,
moneyIn.`Date`,
moneyIn.Customer,
moneyIn.Amount,
FROM moneyIn
我希望金额总和显示在两张表中customer
`相同的发票中,而moneyIn的日期晚于发票日期和(moneyIn中的TranscationNo与transcationNo相同) in Invoice或TranscationNo in moneyIn is Null)
我提出了这个问题:
SELECT
Invoices.id,
Invoices.`Date`,
Invoices.TranscationNo
Invoices.Customer,
Invoices.SaleAmount,
Invoices.Commission,
Invoices.Freight,
Invoices.Total,
(select IFNULL(SUM(moneyIn.Amount),0) FROM moneyIn WHERE Invoices.Customer =moneyIn.Customer AND Invoices.`Date`< moneyIn.`Date`
AND ( Invoices.TranscationNo=moneyIn.TranscationNo or moneyIn.TranscationNo is null )
)AS `TTlTransfert`
FROM Invoices
LEFT outer JOIN moneyIn ON Invoices.Customer = moneyIn.Customer
GROUP BY Invoices.TranscationNo
ORDER BY Invoices.id DESC
但这仍然比发票更早显示金额 我想只显示金额的金额,其日期晚于发票日期 请帮忙 我希望结果如下:
moneyIn
id customer TransactionNo date Amount
1 a dddd 12/19/2016 100
2 a dd55 11/10/2016 300
3 a 10/18/2016 5000
4 b dd4 12/19/2016 1000
5 b d5 8/11/2016 2000
Invoices
id customer TransactionNo Date moneyIn
1 a dddd 12/16/2016 5100
2 a dd55 11/9/2016 300
3 a 65655 12/20/2016 0
答案 0 :(得分:0)
请提供原始数据样本和预期结果,但到目前为止,这是我的尝试:
SELECT
Invoices.id,
Invoices.`Date`,
Invoices.TranscationNo,
Invoices.Customer,
Invoices.SaleAmount,
Invoices.Commission,
Invoices.Freight,
Invoices.Total,
SUM(moneyIn.Amount) AS `TTlTransfert`
FROM Invoices
LEFT JOIN moneyIn
ON Invoices.Customer = moneyIn.Customer
AND Invoices.TranscationNo = moneyIn.TranscationNo
AND Invoices.`Date`< moneyIn.`Date`
GROUP BY Invoices.id, Invoices.TranscationNo
ORDER BY Invoices.id DESC