对于mysql计数,年,月分组的结果不正确

时间:2014-10-06 19:21:12

标签: mysql

我遇到了查询计数错误的问题,如下所示:

mysql> SELECT DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') AS month,
YEAR(FROM_UNIXTIME(timestamp)) AS year, count(*) AS count FROM table1 WHERE
result LIKE '%created%' GROUP BY YEAR(FROM_UNIXTIME(timestamp)),
MONTH(FROM_UNIXTIME(timestamp)) DESC;

+-----------+------+-------+
| month     | year | count |
+-----------+------+-------+
| September | 2011 |     1 |
| December  | 2013 |   393 |
| November  | 2013 |    70 |
| September | 2014 |   233 |
| August    | 2014 |   739 |
| July      | 2014 |   691 |
| June      | 2014 |   618 |
| May       | 2014 |   120 |
| March     | 2014 |   272 |
| February  | 2014 |   528 |
| January   | 2014 |   607 |
+-----------+------+-------+
11 rows in set (0.40 sec)

但是,当我使用其他语法检查我的工作时,我会得到不同的结果:

mysql> select count(*) from table1 where result like '%created%' and
timestamp >= unix_timestamp('2014-07-01') and timestamp <= unix_timestamp('2014-07-31');
+----------+
| count(*) |
+----------+
|      662 |
+----------+
1 row in set (0.39 sec)

语法有什么问题?

1 个答案:

答案 0 :(得分:3)

在第二个查询中使用&lt; UNIX_TIMESTAMP(&#39; 2014-08-01&#39)。我想你过滤掉了2014-07-31 00:00:00之后发生的一切。那天还有24小时不应该被过滤。