MS-Access 中的 SQL:使用 COUNT、JOIN 并返回 0

发布于 2024-09-13 01:17:04 字数 728 浏览 5 评论 0原文

很抱歉发布此内容,但尽管网站上有一些示例,但我无法让我的示例正常工作。

所以我有两个表,如下所示:

电话表

ID | Name | GradeID
1    Richard   1
2    Allan     1
3    Peter

我还有一个成绩表:

ID | Name
1    1
2    2
3    3
4    4
5    5

无论如何,我尝试使用 COUNT() 和 LEFT JOIN 来找出每个成绩在电话表中找到的次数,包括返回任何为 0,通过使用以下查询:

SELECT telephony.GradeID, COUNT(*) AS Total
FROM telephony LEFT JOIN grade 
ON telephony.GradeID = grade.ID
GROUP BY telephony.GradeID
ORDER BY 1;

此查询返回所有找到的内容,但不会返回包含 0 个条目的所有成绩:

Grade | Total
1       2

请帮忙。我正在使用 Microsoft Access 2003。


感谢您的帮助。效果很好。

但是,当我尝试合并日期之间时,它仅返回再次找到的成绩。

有什么想法吗?

谢谢

Apologies for posting this but although there are a few examples on the site, I just can't get mine to work.

So I have two tables as follows:

A Telephony table

ID | Name | GradeID
1    Richard   1
2    Allan     1
3    Peter

I also have a Grade table:

ID | Name
1    1
2    2
3    3
4    4
5    5

Anyway I'm trying to use COUNT() and LEFT JOIN to find out the number of times each grade is found in the Telephony table, including returning any which are 0, by using the following query:

SELECT telephony.GradeID, COUNT(*) AS Total
FROM telephony LEFT JOIN grade 
ON telephony.GradeID = grade.ID
GROUP BY telephony.GradeID
ORDER BY 1;

This query returns all found but will not return all grades with 0 entries:

Grade | Total
1       2

Please help. I'm using Microsoft Access 2003.


Thanks for all your help. That's working great.

However, when I try to incorporate a DATE BETWEEN it returns only the grades found again.

Any ideas?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光病人 2024-09-20 01:17:04

我认为这就是您正在寻找的:

SELECT grade.ID, Count(telephony.ID) AS CountOfID
FROM grade LEFT JOIN telephony ON grade.ID= telephony.GradeID
GROUP BY grade.ID
ORDER BY 1;

顺便说一句:“名称”对于列来说是一个非常糟糕的名称,因为它是保留字。

I think this is what you are looking for:

SELECT grade.ID, Count(telephony.ID) AS CountOfID
FROM grade LEFT JOIN telephony ON grade.ID= telephony.GradeID
GROUP BY grade.ID
ORDER BY 1;

BTW: "Name" is a very bad name for a column since it is a reserved word.

庆幸我还是我 2024-09-20 01:17:04

由于只有 Grade 表包含所有可用 ID,因此您应该在 SELECT 中使用 Grade.ID 而不是 telephony.GradeID。尝试执行此查询:

SELECT grade.ID, COUNT(telephony.ID) FROM grade
LEFT JOIN telephony ON telephony.GradeID = grade.ID
GROUP BY grade.ID
ORDER BY 1;

As only Grade table contains all available IDs you should use grade.ID in your SELECT instead of telephony.GradeID. Try to execute this query:

SELECT grade.ID, COUNT(telephony.ID) FROM grade
LEFT JOIN telephony ON telephony.GradeID = grade.ID
GROUP BY grade.ID
ORDER BY 1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文