简体   繁体   中英

Incorrect results for year,month grouping for mysql count

I am having an issue with incorrect counts for a query which is as follows:

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)

However, when I check my work by using another syntax I get different results:

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)

What is wrong with the syntax?

In the second query use < unix_timestamp('2014-08-01'). I think you filter out everything that happened after 2014-07-31 00:00:00 am. There are still 24 hours in that day that shouldn't be filtered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM