父表:加载和2个子表:发票& 付款即可。 所有链接在Load_id上。 获取每个负载的所有发票和付款的总和。
这给了我错误:[错误] 1054 - 未知栏' I.Load_Id'在' on条款'
SELECT L.Load_Id,
I.Invoice_Total,
P.Payment_Total
FROM Loads AS L
LEFT OUTER JOIN
(
SELECT
SUM(Invoice_Total) AS Invoice_Total
FROM invoices
GROUP BY Load_Id
) AS I
ON I.Load_Id = L.Load_Id
LEFT OUTER JOIN
(
SELECT
SUM(Amount) AS Payment_Total
FROM Payments
GROUP BY Load_Id
) AS P
ON P.Load_Id = L.Load_Id
这也给了我错误:[Err] 1054 - 未知栏' I.Load_Id'在' on条款'
SELECT Loads.Load_Id,
I.Invoice_Total,
P.Payment_Total
FROM Loads
LEFT JOIN
(
SELECT
SUM(Invoice_Total) AS Invoice_Total
FROM invoices
GROUP BY Load_Id
) AS I
ON I.Load_Id = Loads.Load_Id
LEFT JOIN
(
SELECT
SUM(Amount) AS Payment_Total
FROM Payments
GROUP BY Load_Id
) AS P
ON P.Load_Id = Loads.Load_Id
我尝试了很多变化,但显然我没有得到它。
答案 0 :(得分:1)
您没有在子查询中选择任何名为Load_Id
的字段,而mySQL正在抱怨这一点。你确实已经按照它进行分组,所以只需要在下面的查询中选择它就可以加入它。
SELECT L.Load_Id,
I.Invoice_Total,
P.Payment_Total
FROM Loads AS L
LEFT OUTER JOIN
(
SELECT
SUM(Invoice_Total) AS Invoice_Total, Load_Id
FROM invoices
GROUP BY Load_Id
) AS I
ON I.Load_Id = L.Load_Id
LEFT OUTER JOIN
(
SELECT
SUM(Amount) AS Payment_Total, Load_Id
FROM Payments
GROUP BY Load_Id
) AS P
ON P.Load_Id = L.Load_Id