0

我一直在尝试进行一个通用查询,我将在其中获得(Count of number of posts being submitted each day (separately) in a respective month count(postid))

就像让我们假设月份是一月,所以查询返回如下内容。

一个 sql 查询的输出:

日 : 帖子数

1 5

2 3

3 9

等等..

请让我知道什么是正确的方法或任何其他好的通用替代方法,我试图避免进行多次查询来计算每一天的帖子。

表结构: ID | postid | post | date

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

2 回答 2

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
于 2012-11-08T20:45:22.277 回答
0

假设您正在使用date名为的列类型nibbles

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

更新

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

SELECT DAY(`date`) AS day, COUNT(post_id) AS post_count
FROM sparkles
WHERE YEAR(`date`) = 2012 AND MONTH(`date`) = 1
GROUP BY DAY(`date`)
于 2012-11-08T20:45:13.947 回答