Group BY Date计算的数量

时间:2013-12-31 19:19:40

标签: mysql date group-by

我在 mysql 数据库中有一个示例表,如下所示:

id       dateAdded
----------------------------
1        2013-12-24 03:03:19
2        2013-12-24 03:04:19
3        2013-12-24 03:06:14
4        2013-12-24 03:07:23
5        2013-12-25 03:04:19
6        2013-12-26 03:02:19
7        2013-12-26 03:03:19

我想COUNT ids有多少date具有相同的date Count ----------------------- 2013-12-24 4 2013-12-25 1 2013-12-26 2 ,因此结果将是:

{{1}}

请你帮我解决 Mysql 查询

3 个答案:

答案 0 :(得分:0)

简单地按DATE分组(dateAdded)应该可以正常工作;

SELECT DATE(dateAdded) date, COUNT(*) Count
FROM sample_table
GROUP BY date
ORDER BY date 

An SQLfiddle to test with

答案 1 :(得分:0)

您必须先使用date()date_format()将其设置为YYYY-MM-DD格式,然后再将其分组:

SELECT
    count(*)
    ,date_format(dateAdded, '%Y-%m-%d') as dateAdded1
    // ,date(dateAdded) as dateAdded2
FROM
    TABLENAME
GROU BY
    dateAdded1 desc

答案 2 :(得分:0)

SELECT DATE_FORMAT(`dateAdded`, '%Y-%m-%d') AS date, COUNT(*) AS count
FROM table
GROUP BY date