i have table category the format is like this
|id|parent_id|name|
|1 |0 |sport|
|2 |0 |music|
|3 |1 |Stadium|
|4 |1 |Golf|
|5 |2 |Theater|
|6 |2 |Cinema|
Desc
parent_id = 0 is Main category
parent_id != 0 is Sub category and the main Category is Parent_id
and this is the table users
|id|name|category_id|
|1 |andrea|6 |
|2 |Michael | 5 |
|3 |Gorchuf | 1 |
The Problem
selection Category that is Include on Users table and showing only main category example the goal will be like this
|id|name|total|
|1 |sport |1 |
|2 |music |2 |
i’m try with this query
SELECT C.id C.name, count( U.category_id ) AS total
FROM categories C
LEFT JOIN users U ON ( U.category_id = C.id )
GROUP BY C.id
LIMIT 0 , 30
but the result is include sub category and main category
|id|name|total|
|1 |cinema |1 |
|2 |theater|1 |
|3 |sport |1 |
How can I filter that only main category?
QUERY:
SQLFiddleExample
Result: