If I search Pro
I need to get data like Electronics > Mobile > Iphone > Pro
.
That means,
Pro
parent_id
is 3
sibling_order
3
is Iphone
.
Iphone
parent_id
is 2
sibling_order
2
is Mobile
.
Mobile
parent_id
is 1
sibling_order
1
is Electronics
.
Another example:
If I search Mobile
then I need to get data like Electronics/Mobile
.
I tried recursive query but it is not working Syntax error
.
WITH RECURSIVE category_path (id, name, path) AS
(
SELECT id, name, name as path
FROM categories
WHERE parent_id IS NULL
UNION ALL
SELECT c.id, c.name, CONCAT(cp.path, ' > ', c.name)
FROM category_path AS cp JOIN categories AS c
ON cp.id = c.parent_id
)
SELECT * FROM category_path
ORDER BY path;
Any help would be appreciated.
id name sibling_order parent_id
1 Electronics 1 NULL
2 Mobile 2 1
3 Iphone 3 2
4 Pro 4 3
5 SE 5 3
6 XR 6 3
7 Samsung 7 2
8 Galaxy 8 8
9 Note 9 8
10 Home & Furniture 10 NULL
11 Kitchen Storage 11 11
12 Lunch Box 12 12
13 Living Room Furniture 13 11
14 Sofa 14 13