I have this menu table, this table contain menu, and its parent.
The problem is i want to get how many child each menu have. Like this,
I have this menu table, this table contain menu, and its parent.
The problem is i want to get how many child each menu have. Like this,
If you're just interested in how many child menus are directly under a main parent menu?
Then joining those with parent_id = 0 to the childmenus, and grouping on main menus should do.
For example:
SELECT MainMenu.menu_name AS Menu, COUNT(ChildMenu.menu_id) as Child
FROM YourMenuTable AS MainMenu
LEFT JOIN YourMenuTable AS ChildMenu ON ChildMenu.parent_id = MainMenu.menu_id
WHERE MainMenu.parent_id = 0
GROUP BY MainMenu.menu_id, MainMenu.menu_name
ORDER BY MainMenu.menu_name;
Test on Sql Fiddle here