I keep product categories with parentid logic. I need to get all products under a category, but from top to down hierarchy. For example I have these tables:
ProductCategories
id CategoryID ProductID
1 4 1
2 5 2
3 6 3
4 8 4
5 8 5
6 9 5
7 9 2
Categories
ID CategoryName ParentID
1 Kids NULL
2 Accesories 1
3 Shoes 2
4 Flat Shoes 3
5 Leather Sandals 4
6 Sneakers 3
7 Clothing 1
8 T-Shirts 7
9 Bestsellers 1
Products
ID ProductName
1 White Espadrilles
2 Tan Leather Sandals
3 Beige Sneakers
4 Linen T-Shirt
5 Cotton T-Shirt
I use below Sql recursive query:
with CTE as
(
select c.ID,c.ParentID,c.CategoryName,p.ProductName
from Categories c
join ProductCategories pc
on pc.CategoryID=c.ID
join Products p
on pc.ProductID=p.ID
where c.ID = 5 --start id
union all
select ce.ID,ce.ParentID,ce.CategoryName,p.ProductName
from Categories ce
join ProductCategories pc
on pc.CategoryID=ce.ID
join Products p
on pc.ProductID=p.ID
join CTE
on ce.ID=CTE.ParentID
)
select * from CTE
Above query returns below result for a given CategoryID = 5 :
ID ParentID CategoryName ProductName
5 4 Leather Sandals Tan Leather Sandals
4 3 Flat Shoes White Espadrilles
if categoryID = 1 or 2 no record comes.
I dont have a product directly for Accesories category, but I have for it's child categories, so I should get all products under that, from top to down.
How can I do this?