I have the following query:
SELECT
c.frm_category_id,
c.name,
c.slug,
s.frm_category_id,
s.name,
s.slug,
IFNULL(COUNT(t.frm_topic_id),0)
FROM
frm_categories AS c
LEFT JOIN
frm_categories AS s
ON
c.frm_category_id = s.parent_frm_category_id
LEFT JOIN
frm_topics AS t
ON
s.frm_category_id = t.frm_category_id
WHERE
c.active = 1
AND
s.active = 1
ORDER BY
c.frm_category_id ASC,
s.frm_category_id ASC
What I want to accomplish is the following:
I want to select ALL parent categories (even if they don't have subcategories), and I want to count all the topics in every subcategory (a topic can only be posted in a subcategory, not in a category).
This query works half: it only selects the categories with subcategories, and only the subcategories which have topics in it.
Can someone fix this problem? Or give me a useful tip to solve this?
Thanks in advance!