我如何计算个别日期的数量“mytable”

时间:2014-01-28 21:59:31

标签: mysql

mysql> DESCRIBE mytable;
+-----------+-------------------+------+-----+-------------------+----------------+
| Field     | Type              | Null | Key | Default           | Extra          |
+-----------+-------------------+------+-----+-------------------+----------------+
| id        | int(255)          | NO   | PRI | NULL              | auto_increment |
| date      | timestamp         | NO   |     | CURRENT_TIMESTAMP |                |
+-----------+-------------------+------+-----+-------------------+----------------+

mysql> SELECT * FROM mytable;
+----+---------------------+
| id | date                |
+----+---------------------+
|  8 | 2014-01-23 23:59:37 |
|  9 | 2014-01-23 00:00:12 |
| 10 | 2014-01-24 00:00:22 |
| 11 | 2014-01-24 00:00:23 |
| 12 | 2014-01-24 00:00:24 |
| 13 | 2014-01-25 00:00:24 |
| 14 | 2014-01-25 00:00:29 |
+----+-----------+---------+

表我需要:

+-------+------------+
| Count | date       |
+-------+------------+
|  2    | 2014-01-23 |
|  3    | 2014-01-24 |
|  2    | 2014-01-24 |
+-------+------------+

因为有:

2014-01-23日期的两项记录

2014年1月24日的三次记录

2014-01-25日期的两条记录

我的解决方案不完整:

SELECT COUNT(id)AS count,date FROM record_table WHERE ...;

1 个答案:

答案 0 :(得分:1)

只需使用GROUP BY按日期分组:

SELECT DATE(date) AS 'date', COUNT(id) AS 'count'
FROM record_table
GROUP BY DATE(date)