每天一个sql查询所有帖子的数量

时间:2012-11-08 20:41:36

标签: mysql count

我一直试图制作一个通用查询,我会得到(Count of number of posts being submitted each day (separately) in a respective month count(postid))

比如让我们假设月份是1月,所以查询会返回类似下面的内容。

来自一个SQL查询的输出:

Day:Nummber of posts

1 5

2 3

3 9

依旧......

请告诉我这是一种正确的做法或任何其他良好的通用替代方法,我试图避免多次查询以计算每个日期的帖子。

表格结构: ID | postid | post | date

Select count(postid) from posts where month="JAN"

2 个答案:

答案 0 :(得分:1)

在不知道数据库结构如何的情况下,通用查询看起来像这样:

SELECT YEAR(postdate) AS year, MONTH(postdate) AS month,
    DAY(postdate) AS day, COUNT(postid) AS count
FROM posts
GROUP BY YEAR(postdate), MONTH(postdate), DAY(postdate)
ORDER BY year, month, day

答案 1 :(得分:0)

假设您使用名为date的{​​{1}}列类型:

nibbles

<强>更新

使用OP更新的表格结构......

SELECT DAY(nibbles) AS day, COUNT(*) AS post_count
FROM sparkles
WHERE YEAR(nibbles) = 2012 AND MONTH(nibbles) = 1
GROUP BY DAY(nibbles)