简体   繁体   中英

one sql query for all count of posts each day

I have been trying to make one generic query in which i'll get the (Count of number of posts being submitted each day (separately) in a respective month count(postid))

Like lets suppose month is january so query returns something like following.

Output from one sql query:

Day : Nummber of posts

1 5

2 3

3 9

and so on..

Kindly let me know what is a right way to do that or any other good generic alternative, I am trying to avoid making multiple queries to count post for each respective day.

Table Structure: ID | postid | post | date ID | postid | post | date

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

Without knowing how your DB is structured, a generic query woudl look something like this:

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

Assuming you're using a date column type named nibbles :

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

Update

Using OP's updated table structure...

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`)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM