There was a previous question on StackOverflow about this subject (can insert the link, I've got no privileges for the moment) entitled "MySQL date comparison filter", and this goes as an extension of that one.
I've got some authors on my WordPress blog and I would like to get their productivity through MySQL. The next query works pretty well under MySQL to get an author's post during certain time range only one day:
SELECT SQL_CALC_FOUND_ROWS wp_posts.*
FROM wp_posts
JOIN wp_postmeta
ON (wp_posts.ID = wp_postmeta.post_id)
WHERE wp_posts.post_type = 'post'
AND post_author = '50'
AND post_date
BETWEEN STR_TO_DATE('2011-10-27 14:19:17','%Y-%m-%d %H:%i:%s')
AND STR_TO_DATE('2011-10-27 14:51:17','%Y-%m-%d %H:%i:%s')
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 0, 100
But it gives me just the posts of that day during that hour range. I'd like to get a table with with everyday data filled up for each day and each author. On each day and each author, there should be the number of posts published by that author on that day and on that hour range.
The output should be something like this:
October Auth1 Auth2 Auth3
1 0 0 0
2 0 0 0
3 0 1 0
4 0 2 0
5 1 0 0
6 0 2 0
7 0 0 0
8 3 0 0
9 0 0 0
10 5 1 0
11 1 0 0
...
31 2 1 1
So the date should be a variable, but I'd like to include all authors, so I'd remove the post_author AND line.
I'm no expert at MySQL but I wonder if this could be done more or less easily and export the query results (or, more exactly, some fields of the query results) as a table, like the one shown.
I'm not going to rewrite your entire query, but here's how you'd do the data grouping:
Creating multiple columns for each author is not a good use of a query. That sort of transformation is better done in your wordpress code.
Note that this query will work on exact clock periods, 1am, 2am, 3am, etc... If you need arbitrary times (1:05am, 2:05am, 3:05am, etc...), this won't work and you'll need a more complicated grouping.
I reckon you should create a date reference table, populate that table and then
LEFT OUTER JOIN
from that table in your query. The problem of 'How Do I Display Missing Dates?' is quite a common SO question but I'll go for it anyway.Prelim Step
At the mysql prompt run:
Step 1 - Create Date Reference Table
Step 2 - Populate Date Reference Table
The idea of this table is to have one row for every date. Now you could achieve this by running insert statements ad nauseum but why not write a routine to populate it for you (you could event create a MySQL scheduled event to ensure that you always have a complete set of dates in the table. Here's a suggestion for that routine:
You can now run:
To populate all the dates for October (or just crank up the
days_into_the_future
parameter to whatever you want).Now that you have a date reference table with all dates that you're interested in populated you can go ahead and run your query for October:
The query below will group number of posts by author.
This will output a table like:
meaning for line 1 for example: on 2 October 2011 author with id 50 has 23 posts. You would first fetch the authors in an array to form the header of your table. Then iterating this result with PHP you could generate a table like you want it accordingly placing 's and 's.