英文:
Problem in creating the query containing a CROSS JOIN followed by a LEFT JOIN in Microsoft Access
问题
我试图将以下的SQLite查询转换成MS-ACCESS(Microsoft Access 2016)查询,但没有成功:
SQLite中的查询:
SELECT c.CategoryName, s.SubCategoryName, COUNT(l.LoanDate) AS Count
FROM LoanCategories c CROSS JOIN LoanSubCategories s
LEFT JOIN Loans l
ON l.LoanCatId = c.CategoryId AND l.LoanSubCatId = s.CategoryId AND l.LoanDate = '19990125'
GROUP BY c.CategoryName, s.CategoryName;
我尝试了以下MS-ACCESS中的查询,但出现了“JOIN操作中的语法错误”:
MS-ACCESS中的查询:
SELECT c.CategoryName, s.SubCategoryName, COUNT(l.LoanDate) AS Count
FROM LoanCategories c, LoanSubCategories s
LEFT JOIN Loans l
ON l.LoanCatId = c.CategoryId AND l.LoanSubCatId = s.CategoryId AND l.LoanDate = '19990125'
GROUP BY c.CategoryName, s.CategoryName;
如何在MS-ACCESS中重新编写这个查询?
英文:
I'm trying to convert the following SQLite
query to a MS-ACCESS
(Microsoft Access 2016) query without any success:
query in SQLite
:
SELECT c.CategoryName, s.SubCategoryName, COUNT(l.LoanDate) AS Count
FROM LoanCategories c CROSS JOIN LoanSubCategories s
LEFT JOIN Loans l
ON l.LoanCatId = c.CategoryId AND l.LoanSubCatId = s.CategoryId AND l.LoanDate = '19990125'
GROUP BY c.CategoryName, s.CategoryName;
I've tried the following query in MS-ACCESS
which leads to the 'Syntax error in JOIN operation' error:
my query in MS-ACCESS
:
SELECT c.CategoryName, s.SubCategoryName, COUNT(l.LoanDate) AS Count
FROM LoanCategories c, LoanSubCategories s
LEFT JOIN Loans l
ON l.LoanCatId = c.CategoryId AND l.LoanSubCatId = s.CategoryId AND l.LoanDate = '19990125'
GROUP BY c.CategoryName, s.CategoryName;
How could I rewrite the query in MS-ACCESS
?
答案1
得分: 1
以下是翻译好的部分:
MS-Access中的语法非常不同。
不支持显式的CROSS
连接,必须使用旧的逗号语法。
此外,连接的ON
子句不能包含像LoanDate = '19990125'
这样的表达式。
查询可以这样编写:
SELECT t.CategoryName, t.SubCategoryName, Nz(l.Count, 0) AS Count
FROM (
SELECT c.CategoryId, c.CategoryName,
s.CategoryId AS SubCategoryId, s.CategoryName AS SubCategoryName
FROM LoanCategories AS c, LoanSubCategories AS s
) AS t
LEFT JOIN (
SELECT LoanCatId, LoanSubCatId, COUNT(*) AS Count
FROM Loans
WHERE LoanDate = '19990125'
GROUP BY LoanCatId, LoanSubCatId
) AS l ON l.LoanCatId = t.CategoryId AND l.LoanSubCatId = t.SubCategoryId;
英文:
The syntax in MS-Access is quite different.<br/>
The explicit CROSS
join is not supported and you must use the old comma-syntax.<br/>
Also, the ON
clause of a join can't contain expressions like LoanDate = '19990125'
.<br/>
The query could be written like this:
SELECT t.CategoryName, t.SubCategoryName, Nz(l.Count, 0) AS Count
FROM (
SELECT c.CategoryId, c.CategoryName,
s.CategoryId AS SubCategoryId, s.CategoryName AS SubCategoryName
FROM LoanCategories AS c, LoanSubCategories AS s
) AS t
LEFT JOIN (
SELECT LoanCatId, LoanSubCatId, COUNT(*) AS Count
FROM Loans
WHERE LoanDate = '19990125'
GROUP BY LoanCatId, LoanSubCatId
) AS l ON l.LoanCatId = t.CategoryId AND l.LoanSubCatId = t.SubCategoryId;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论