MySQL

On the Internet you can find many examples of the construction of trees in different bundles of php and MySQL, will not discuss the quality of assigned tasks. There is a saying – "How to understand the code of another person, it is better to write itself. So I decided to write the code, but used entirely for the construction and grading of the tree only means of MySQL. Immediately make a reservation in the presented cheap football jerseys version of the tree of three levels, without claim of its uniqueness. We describe the situation: There is a table of categories, containing the following fields: id (code) id_group (code top group) name (name) must be using a query to build a sorted table that contains the source table and field such as: level (level) veight (weight, number of nodes or cheap oakleys leaves subordinates current site, will be wholesale football jerseys china used to display the tree) to sort it took us more and more fields: top_group (site code first) sub_group (site code of the second level) and the expanded query tree sorted by name: select t_top.id, t_top.id_group, t_top.name, t_top.level, if (t_top.level = 1, t_top.id, if (t_top.level = 2, t_top.id_group, (SELECT c_. id_group from categories c_ where c_. id = t_top.id_group))) as top_group, if (t_top.level = 1, 0, if (t_top.level = 2, t_top.id, t_top.id_group)) as sub_group, (select count (*) from categories where id_group = t_top.id) as veight From / * In this section, just get the levels & * / (select c.id, c.id_group, c.name, 1 as level from categories Cheap Oakleys Sunglasses c where c.id_group = 0 union all select cc.id, cc.id_group, cc.name, two as level from categories cc where cc.id_group in (select c.id from categories c where c.id_group = 0 order by c.name) union all select ccc.id, ccc.id_group, ccc.name, three as level from categories ccc where ccc.id not in (select c.id from categories c where c.id_group = 0 order by c.name) and ccc.id not in (select cc.id from categories cc where cc.id_group in (select c.id from categories c where c.id_group = 0 order by wholesale nfl jerseys c.name))) t_top order by top_group, sub_group, level, name for the construction of the collapsed tree, for some sites will not be difficult to insert several conditions.