-1

I have this menu table, this table contain menu, and its parent.

enter image description here

The problem is i want to get how many child each menu have. Like this,

enter image description here

Strawberry
  • 33,750
  • 13
  • 40
  • 57
Josh Parinussa
  • 633
  • 2
  • 11
  • 28

1 Answers1

1

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

LukStorms
  • 28,916
  • 5
  • 31
  • 45