我正试图在销售系统中获得出色的平衡。我通过组合3个表来实现这一点,因为它有多对多的关系。根据MSDN
“LEFT JOIN或RIGHT JOIN可以嵌套在INNER JOIN中,但是 INNER JOIN不能嵌套在LEFT JOIN或RIGHT JOIN“
中
但是当我运行查询时,我收到错误"JOIN expression not supported"
这是我的代码:
SELECT DISTINCTROW Table1.CustomerName, Sum(Table1.TotalAmount) AS [Total Payable Amount]
, Sum(Table2.PaidAmount) AS [Total Paid Amount]
, Sum(Table1.TotalAmount - Table2.PaidAmount) AS [Total Outstanding Balance]
FROM Table2
INNER JOIN
(Table1 LEFT JOIN Table3 ON Table1.InvoiceNumber =Table3.InvoiceNumber)
ON Table2.ReceiptNumber = Table3.ReceiptNumber
GROUP BY Table1.CustomerName;
答案 0 :(得分:1)
一种选择是将LEFT JOIN
替换为INNER JOIN
,并修改ON
条件以保留Table1
中不匹配的行,从而模拟LEFT JOIN
}:
SELECT DISTINCTROW Table1.CustomerName, Sum(Table1.TotalAmount) AS [Total Payable Amount],
Sum(Table2.PaidAmount) AS [Total Paid Amount]
Sum(Table1.TotalAmount - Table2.PaidAmount) AS [Total Outstanding Balance]
FROM Table2
INNER JOIN
(
SELECT Table1.*. Table3.ReceiptNumber
FROM Table1 INNER JOIN Table3
ON Table1.InvoiceNumber = Table3.InvoiceNumber
UNION
SELECT Table1.*, NULL
FROM Table1
WHERE Table1.InvoiceNumber NOT IN (SELECT Table3.InvoiceNumber FROM Table3)
)
ON Table2.ReceiptNumber = Table3.ReceiptNumber
GROUP BY Table1.CustomerName;
答案 1 :(得分:0)
我总是在使用MS Access SQL。但是在桌面设计师的帮助下,我认为我发现了你的语法错误。尝试将FROM子句更改为:
FROM
(
Table2 INNER JOIN Table3 ON Table2.ReceiptNumber = Table3.ReceiptNumber
)
LEFT JOIN Table1 ON Table3.InvoiceNumber = Table1.InvoiceNumber;
这里的关键是重构您的查询,以便INNER连接出现在括号内,LEFT连接出现在外面。