I'm struggling to create a single query to retrieve all descendants for a given parent id. I have the following table structure:
menu_id | parent_id | title
1 0 Text 1
2 0 Text 2
3 2 Text 2.1
4 2 Text 2.2
5 2 Text 2.3
6 0 Text 3
7 6 Text 3.1
8 6 Text 3.2
9 6 Text 3.3
10 6 Text 3.4
11 10 Text 3.4.1
12 10 Text 3.4.2
13 10 Text 3.4.3
14 10 Text 3.4.4
I want to achieve the following result, for given parent id 6:
menu_id
6
7
8
9
10
11
12
13
14
Yet, I'm not able to go trough all descendants, but to the first node.. This is the query that I managed to create until now:
SELECT T2.menu_id
FROM (
SELECT
@r AS _id,
(SELECT @r := menu_id FROM bo_admin_menu WHERE parent_id = _id) AS menu_id,
@l := @l + 1 AS lvl
FROM
(SELECT @r := 6, @l := 0) vars,
bo_admin_menu h
WHERE @r <> 0) T1
JOIN bo_admin_menu T2
ON T1._id = T2.parent_id
ORDER BY T1.lvl DESC
Could someone point me to the correct path?