计算行数时遇到问题

发布于 2024-12-27 11:59:36 字数 662 浏览 5 评论 0原文

我使用日历表作为连接表,并尝试创建一个图表来显示员工从 CMS 收到的评论数量。查询当前看起来像这样

SELECT MONTHNAME(Calendar.datefield) AS Month, COUNT(Review.reviewee_id) AS Count
FROM calendar Calendar 
LEFT JOIN reviews Review ON MONTH(Calendar.datefield) = MONTH(Review.created) AND Review.reviewee_id = 24 AND YEAR(Calendar.datefield) = '2011'
GROUP BY Month
ORDER BY MONTH(Calendar.datefield)

This returns

'January', '0'
'February', '0'
'March', '0'
'April', '0'
'May', '0'
'June', '0'
'July', '0'
'August', '0'
'September', '0'
'October', '434'
'November', '120'
'December', '0'

但计数错误。我试图了解计数的工作方式。每个月的正确率为 0,但该员工 10 月份只有 4 条评论。

I'm using a calendar table as a join table and trying to create a chart for the number of reviews an employee would have received from a CMS. The query currently looks like this

SELECT MONTHNAME(Calendar.datefield) AS Month, COUNT(Review.reviewee_id) AS Count
FROM calendar Calendar 
LEFT JOIN reviews Review ON MONTH(Calendar.datefield) = MONTH(Review.created) AND Review.reviewee_id = 24 AND YEAR(Calendar.datefield) = '2011'
GROUP BY Month
ORDER BY MONTH(Calendar.datefield)

This returns

'January', '0'
'February', '0'
'March', '0'
'April', '0'
'May', '0'
'June', '0'
'July', '0'
'August', '0'
'September', '0'
'October', '434'
'November', '120'
'December', '0'

But the counting is wrong. I'm trying to understand the way the counting is working. It is correct as far as 0 for each month, but for October this employee only had 4 reviews.

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

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

发布评论

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

评论(2

卷耳 2025-01-03 11:59:36

我相信你应该更换
MONTH(日历.日期字段) = MONTH(评论.创建)

Calendar.datefield = Review.created

如果没有该替换,您将计算特定月份(例如 10 月)内发生的所有评论,而不仅仅是 2011 年特定月份的评论,还有 2010 年 10 月的例子。

I believe you should replace
MONTH(Calendar.datefield) = MONTH(Review.created)
with
Calendar.datefield = Review.created

Without that replacement, you count all reviews that happened in a specific Month (for example October), not only those for a specific Month in 2011, but also, for example those in October 2010.

你的笑 2025-01-03 11:59:36

要了解发生了什么,您可以尝试一些更简单的查询。首先尝试取出 GROUP BY 并获取它正在计数的所有内容的列表。

SELECT MONTHNAME(Calendar.datefield) AS Month, Review.reviewee_id, *
FROM calendar Calendar 
LEFT JOIN reviews Review ON MONTH(Calendar.datefield) = MONTH(Review.created) 
AND Review.reviewee_id = 24 AND YEAR(Calendar.datefield) = '2011'
AND MONTHNAME(Calendar.datefield) = 'October'

最有可能的情况是多行链接,但 434 不能均匀地以 4 的方式计算,这很奇怪。

To understand what is happening you can try some simpler queries. First try taking out the GROUP BY and get the list of everything it is counting.

SELECT MONTHNAME(Calendar.datefield) AS Month, Review.reviewee_id, *
FROM calendar Calendar 
LEFT JOIN reviews Review ON MONTH(Calendar.datefield) = MONTH(Review.created) 
AND Review.reviewee_id = 24 AND YEAR(Calendar.datefield) = '2011'
AND MONTHNAME(Calendar.datefield) = 'October'

The most likely thing is that multiple rows are linking, but 434 does not work out evenly in 4's which is odd.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文