Site icon Hip-Hop Website Design and Development

mySQL statment count of post in each category and sub category

im trying to get a count of all post for each Categorty and sub category

it should show

main_cat     10
  -a1       5
  -a2       5 

i have tried this but my counts are all of and im getting categories that are not listed

SELECT t.name as `name`, t1.name as parentName, count(p.ID) as `TotalPosts`,
    SUM(if(p.post_date>'2015-12-01 00:00:00',1,0)) as 'LastMonth' 
    FROM wp_posts p
    LEFT JOIN wp_term_relationships rel ON rel.object_id = p.ID 
    LEFT JOIN wp_term_taxonomy tax ON tax.term_taxonomy_id = rel.term_taxonomy_id AND tax.taxonomy ='category' 

    LEFT JOIN wp_terms t ON t.term_id = tax.term_id 

    LEFT JOIN wp_term_taxonomy tax1 ON tax.parent = tax1.term_taxonomy_id 

    LEFT JOIN wp_terms t1 ON t1.term_id = tax1.term_id 

    WHERE p.post_status = 'publish' 
    GROUP BY t.name
    ORDER BY t1.name,LastMonth DESC, TotalPosts Desc

im trying to recreate the page->category section the same way it is listed