I have to operate a query in Wordpress to obtain the number of posts you will find monthly including zeros.

My query at this time returns part one of the items I'm attempting to return:

select
distinct date_format(post_date, '%y') "year",
date_format(post_date, '%b') "month",
from wp_posts
where post_type = 'post'
and post_status = 'publish'
group by date_format(post_date, '%y'), date_format(post_date, '%b')
order by date_format(post_date, '%y') desc, post_date

It returns something similar to:

|   year   |   month   |   count   |
------------------------------------
|   10     |   Jan     |     4     |
|   10     |   Feb     |     2     |
|   10     |   Mar     |     1     |
|   10     |   Apr     |     6     |
|   09     |   Jan     |     4     |
|   09     |   Feb     |     2     |

I have to return something similar to:

|   year   |   month   |   count   |
------------------------------------
|   10     |   Jan     |     4     |
|   10     |   Feb     |     2     |
|   10     |   Mar     |     1     |
|   10     |   Apr     |     6     |
|   10     |   May     |     0     |
|   10     |   Jun     |     0     |
|   10     |   Jul     |     0     |
|   10     |   Aug     |     0     |
|   10     |   Sep     |     0     |
|   10     |   Oct     |     0     |
|   10     |   Nov     |     0     |
|   10     |   Dec     |     0     |
|   09     |   Jan     |     4     |
|   09     |   Feb     |     2     |
|   09     |   Mar     |     0     |
|   09     |   Apr     |     0     |
|   09     |   May     |     0     |
|   09     |   Jun     |     0     |
|   09     |   Jul     |     0     |
|   09     |   Aug     |     0     |
|   09     |   Sep     |     0     |
|   09     |   Oct     |     0     |
|   09     |   Nov     |     0     |
|   09     |   Dec     |     0     |

I'd use rownum in Oracle, however i can't determine the query using MySQL syntax. Any assistance is greatly appreciated.

This involves the amounts table trick -

  1. Produce a table known as Amounts:

    DROP TABLE IF EXISTS `example`.`numbers`;
    CREATE TABLE  `example`.`numbers` (
      `id` int(10) unsigned NOT NULL auto_increment,
       PRIMARY KEY  (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
  2. Populate the table by running the next a minimum of 24 occasions:

    INSERT INTO NUMBERS (id) VALUES (NULL)
    

That will help you to generate a listing of dates using:

SELECT DATE(DATE_ADD('2009-01-01', INTERVAL n.id MONTH)) AS dt
  FROM NUMBERS n

Now, you are able to left join your present query towards the listing of dates:

   SELECT DATE_FORMAT(x.dt, '%y') "year",
          DATE_FORMAT(x.dt, '%b') "month",
          COUNT(*) AS count
     FROM (SELECT DATE_ADD('2009-01-01', INTERVAL n.id MONTH) AS dt
             FROM NUMBERS n) x
LEFT JOIN WP_POSTS wp ON MONTH(wp.post_date) = MONTH(x.dt)
                     AND YEAR(wp.post_date) = YEAR(x.dt)
                     AND wp.post_type = 'post'
                     AND wp.post_status = 'publish'
    WHERE YEAR(x.dt) IN (2009, 2010)
 GROUP BY DATE_FORMAT(x.dt, '%y'), DATE_FORMAT(x.dt, '%b')
 ORDER BY DATE_FORMAT(x.dt, '%y') DESC, MONTH(x.dt)