I have 2 tables
1)master_category
cat_id cat_name
------------------------------
1 Mobiles
2 Motorola
3 Microsoft
4 Mobile Accessories
5 Samsung
6 Sony
7 Samsung Accessories
8 Galaxy S6
9 Laptops
10 HP
11 Sony Vaio
12 Dell
13 Dell Accessories
--------------------------------------------
2)category_group ( In this table grouping category master datas - main_group and Sub_group are cat_ids from master_category table
id Main_group Sub_group
-----------------------------------------------------
1 1 2
2 1 3
3 1 4
4 1 5
5 1 6
6 4 7
7 5 8
8 9 10
9 9 11
10 9 12
11 12 13
---------------------------------------------
in category_group category Mobile have sub categories like motoroal,microsoft,samsung,mobile accessories and mobile acessories have sub category samsung accessories, Also samsung have sub groups galaxy s6
Laptops are also grouped like that
when i search mobiles i need to write a query to get a result as follows
cat_id cat_name
-------------------------------
2 Motorola
3 Microsoft
4 Mobile Accessories
5 Samsung
6 Sony
7 Samsung Accessories
8 galaxy s6
---------------------------------------------
when i search Laptops i need to get a result as follows
cat_id cat_name
----------------------------------
10 HP
11 Sony Vaio
12 Dell
13 Dell Accessories
---------------------------------------------