Mysql count sub categories of a category using mysql join

From Code Trash

Jump to: navigation, search

I have two tables one is the main category and a sub category and under the sub category comes the products.

main table has fields like id, name sub table has fields like id, cid, subname

and i wanted to list main category list with the count of its sub categories like the following

1. Tools (3) 2. Travels (4)

and here is the query i used.

SELECT a.* , count(b.id) AS total
		 FROM merchandise_category a LEFT JOIN
		 merchandise_subcategory b ON a.id=b.cid 
		 GROUP BY a.id ORDER BY a.name
Personal tools
Google AdSense