I'm stucked into a count query.
I have 3 tables:
Articoli
ID | Title | ecc...
Categories
ID | Name | Parent
articles_category
category_id | article_id
Category are recursive, for example I have a main category "News" with 3 sub cat.
I need to count how many article are in "News", but my article are tagged in the "articles_category" table with the subcat ID (if there is a SubCat) or with main Cat ID, if it have no subcat. So far i tried:
SELECT count(a.id), child.name AS child, parent.name AS parent
FROM categories parent
JOIN categories child ON child.parent = parent.tid
JOIN categories_articoli ca ON child.tid = ca.category_id
OR parent.tid = ca.category_id
JOIN articoli a ON a.id = ca.articolo_id
GROUP BY parent.tid
But this return me only the parent cat that have subcategory, but this is everytime true. Any suggestion?