Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6756301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:30:29+00:00 2026-05-26T13:30:29+00:00

There was a previous question on StackOverflow about this subject (can insert the link,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:30:29+00:00Added an answer on May 26, 2026 at 1:30 pm

    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:

    use WordPress;
    

    Step 1 – Create Date Reference Table

    create table all_date 
    (id int unsigned not null primary key auto_increment, 
    a_date date not null,
    last_modified timestamp not null default current_timestamp on update current_timestamp,
    unique key `all_date_uidx1` (a_date));
    

    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:

    DELIMITER //
    
    
    CREATE PROCEDURE populate_all_dates(IN from_date DATE, IN days_into_future INT)
    
    BEGIN
    
     DECLARE v_date DATE;
     DECLARE ix int;
    
    
     SET ix := 0;
     SET v_date := from_date;
    
    
     WHILE v_date <= (from_date + interval days_into_future day) DO
    
      insert into all_date (a_date) values (v_date) 
      on duplicate key update last_modified = now();
    
      set ix := ix +1;
    
      set v_date := from_date + interval ix day;
    
     END WHILE;
    
    END//
    
    DELIMITER ;
    

    You can now run:

    call populate_all_dates('2011-10-01',30);
    

    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:

    select day(a.a_date) as 'October',
    IFNULL(t.a1,0) as 'Auth1',
    IFNULL(t.a2,0) as 'Auth2',
    IFNULL(t.a50,0) as 'Auth50'
    from all_date a
    LEFT OUTER JOIN
    (
    SELECT date(wp.post_date) as post_date,
    sum(case when wp.post_author = '1' then 1 else 0 end) as a1,
    sum(case when wp.post_author = '2' then 1 else 0 end) as a2,
    sum(case when wp.post_author = '50' then 1 else 0 end) as a50,
    count(*) as 'All Auths'
    FROM wp_posts wp
    WHERE  wp.post_type = 'post'
    AND wp.post_date  between '2011-10-01' and '2011-10-31 23:59:59'
    GROUP BY date(wp.post_date)
    ) t
    ON a.a_date = t.post_date
    where a.a_date between '2011-10-01' and '2011-10-31'
    group by day(a.a_date);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is similar to a previous question , but the answers there don't satisfy
This question is related to a previous post . Is there something comparable to
This question is related with one of my earlier questions.. Previous Post In there
There is previous little on the google on this subject other than people asking
I want to continue on my previous question: https://stackoverflow.com/questions/3007168/torrents-can-i-protect-my-software-by-sending-wrong-bytes Developer Art suggested to add
There was a previous question on stackoverflow: Is it possible to start MAIL MERGE
This is closely related to my previous question , which was about using CMake
After my previous question (http://stackoverflow.com/questions/8217522/best-way-to-search-for-partial-words-in-large-mysql-dataset), I've chosen Sphinx as the search engine above my
Continuing from my previous question , is there a comprehensive document that lists all
On a more abstract level then a previous question , in my experience there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.