0
Im做一個查詢在那裏我做2個求和,但它發出了一種怪異的結果SQL 2個求和給予了不正確的結果
SELECT
SUM(Details.price * Details.quantity) AS total_amount,
SUM(Payment.amount_paid) as paid_amount
FROM
Details,
Invoice
LEFT JOIN
Payment ON Invoice.id = Payment.fk_invoice_id
WHERE
AND Invoice.id = 1
AND Invoice.id = Details.fk_invoice_id
GROUP BY Details.fk_invoice_id;
什麼是目前發生的事情是可以說有在支付3行相匹配的選擇標準並且只有1個細節,total_amount中的總和乘以3,但是paid_amount是正確的。然後讓我們說明細節中有2項與條件相符,付款方式爲1,paid_amount乘以2,但total_amount是正確的。任何提示都會很棒,謝謝!
架構
Invoice
id
Name....
Details
id
fk_invoice_id
name
price
quantity
Payment
id
fk_invoice_id
amount_paid
更多細節:
我正在努力簡化我的職位,但只是添加
客戶有許多發票
我只能是一個參考給客戶,所以customer.id是發票中的外鍵,即時嘗試獲取特定客戶的所有發票。