Example data:
+----+-------+----------+
| org_id | Name | ParentID |
+----+-------+----------+
| 1 | Org1 | 2 |
| 2 | Org2 | NULL |
| 3 | Org3 | 5 |
| 5 | Org5 | 1 |
| 14 | Org14 | 1 |
+----+-------+----------+
If I am logged in as user with org_id 1 (Org1). I want to retrieve the full tree of that user.
I have the following recursive Query :
WITH RECURSIVE cte (org_id, name, parent_id) AS (
SELECT org_id, name, parent_id
FROM organization
WHERE org_id = 1
UNION ALL
SELECT t1.org_id, t1.name, t1.parent_id
FROM organization t1
INNER JOIN cte t2 ON t1.parent_id = t2.org_id
)
SELECT * FROM cte;
However, this query only gives me the children of the current id (Org1 in this example). How can I include all the parents as well in the result set, so I can rebuild the entire tree accurately?
EDIT: I am using MariaDB version 10.4.10
EDIT:
I tried the query as in the answer below, i'm getting a syntax error :