[英]SQL count Query retrieving 0
i want to list the amount of views per object_id, between the current date and the date year ago. 我想列出当前日期和一年前的日期之间每个object_id的视图数量。 So, i have this Query: 所以,我有这个查询:
SELECT count(*) FROM event_logs WHERE object_id=252 AND object_type='product' AND event_type='view' AND event_date BETWEEN 2014-02-14 AND 2015-02-14 GROUP BY month(event_date)
And the following table: 和下表:
event_id | user_id | objectd_id | object_type | event_type | event_date
1 | 1 | 252 | product | view | 2014-02-25 00:00:00
2 | 1 | 252 | product | view | 2015-02-12 19:36:05
3 | 1 | 252 | product | view | 2015-01-05 19:36:05
The problem is when i execute the query, show an amount of results of 0 (zero), 问题是当我执行查询时,显示的结果量为0(零),
Could have i been doing wrong? 我可能做错了吗?
Please help and thank you all for your attention! 请帮助并感谢大家的关注!
One problem is the event_date
constants: 一个问题是event_date
常量:
SELECT count(*)
FROM event_logs
WHERE object_id = 252 AND
object_type = 'product' AND
event_type = 'view' AND
event_date BETWEEN 2014-02-14 AND 2015-02-14
------------------------^ ---------^---^----------^ missing quotes
GROUP BY month(event_date);
Date constants should be in single quotes. 日期常量应该用单引号引起来。 Otherwise, these are treated as arithmetic -- 2014 - 2 - 14 = 1998, which is not really a valid date. 否则,这些将被视为算术-2014-2-14 = 1998,这实际上不是有效日期。
Also, if you want the total value, it is unclear why you are grouping by the month. 另外,如果您想要总价值,则不清楚为什么要按月份分组。 You can use CURRENT_DATE
and date arithmetic and not have to hardcode the dates: 您可以使用CURRENT_DATE
和日期算术,而不必对日期进行硬编码:
SELECT count(*)
FROM event_logs
WHERE object_id = 252 AND
object_type = 'product' AND
event_type = 'view' AND
event_date <= CURRENT_DATE and
event_date > date_sub(CURRENT_DATE, interval -1 year);
Depending on the exact logic, you might want to change the <=
to <
or whatever -- depending on whether you want to include the end dates. 根据确切的逻辑,您可能希望将<=
更改为<
或其他内容-取决于是否要包含结束日期。 Note: if event_date
has a time component, then you might want to use date(event_date)
to strip it off. 注意:如果event_date
具有时间成分,则可能要使用date(event_date)
删除它。
改成这个
BETWEEN '2014-02-14 00:00:00' AND '2015-02-14 23:59:59'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.