According to this How to include "zero" / "0" results in COUNT aggregate? I need to left join the tbl_postad table.
Following is my query
This queries shows count of ads in each category.
SELECT c.*, count(a.post_id) as item_count
FROM tbl_category c
LEFT JOIN tbl_postad a on a.post_maincategory = c.cat_id
WHERE c.cat_parent=0 AND c.cat_promote = 0
AND c.selActive = 'Y' AND a.post_delete_status!='Y' AND a.post_cat_status!='N'
AND a.post_status='Y' AND a.block=0 AND a.post_expiredate >= '".date("Y-m-d H:i:s")."'
GROUP BY c.cat_name
ORDER BY c.cat_sort asc
If remove query related to post it shows all the categories:
SELECT c.*, count(a.post_id) as item_count
FROM tbl_category c
LEFT JOIN tbl_postad a on a.post_maincategory=c.cat_id
WHERE c.cat_parent=0 AND c.cat_promote = 0 AND c.selActive = 'Y'
GROUP BY c.cat_name
ORDER BY c.cat_sort asc
But if I add query related to posts then it excludes all the categories with post count of zero.