MySQL查询:按天显示条目数

时间:2011-09-02 15:23:06

标签: mysql

我有一张结构表:

id|name|       date
---------------------------
 1|qwer|2011-08-29 13:11:59
 2|wert|2011-08-29 13:11:59
 3|erty|2011-08-30 13:11:59
 4|rtyu|2011-08-31 13:11:59

我需要一个查询来显示这样的数据:

    day   |number_of_entries
----------------------------
2011-08-29|                2
2011-08-30|                1
2011-08-31|                1

2 个答案:

答案 0 :(得分:3)

使用GROUP BY函数和COUNT函数提供以下内容:

SELECT DATE(`date`), COUNT(*) AS number_of_entires
FROM tablename
GROUP BY DATE(`date`)
ORDER BY `date`

GROUP BY - MySQL Reference Manual

DATE - MySQL Reference Manual

答案 1 :(得分:1)

SELECT   DATE(`date`) AS day,
         COUNT(*)     AS number_of_entries
FROM     mytable
GROUP BY DATE(`date`)

使用MySQL的DATE()功能。