答案 0 :(得分:1)
如果您只是对直接在主父菜单下有多少个子菜单感兴趣?
然后将parent_id = 0的用户加入子菜单,并在主菜单上进行分组。
例如:
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;
在Sql Fiddle here上进行测试