计算行数时遇到问题
我使用日历表作为连接表,并尝试创建一个图表来显示员工从 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信你应该更换
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.
要了解发生了什么,您可以尝试一些更简单的查询。首先尝试取出 GROUP BY 并获取它正在计数的所有内容的列表。
最有可能的情况是多行链接,但 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.
The most likely thing is that multiple rows are linking, but 434 does not work out evenly in 4's which is odd.