After searching a lot here, I could not find solution to my problem. So, I am posting this question.
I have a Database Table which has the structure like this:
folder_id folder_name parent_id
--------------------------------------
1 Private 0
2 Public 0
3 Photos 0
4 December 3
5 Bday 4
6 Celeb 5
In hierarchical form, it will be like a folder structure :
-- Private
-- Public
-- Photos
----- December
-------- Bday
----------- Celeb
Now, I would like to select a Path upto a particular folder, like to Bday or Celeb folder. Thus, I want a MySQL query which will return me only the rows containing the folders between the path to a specific folder.
For Example, If I want a path to Celeb folder, then the Query should return these rows only :
folder_id folder_name parent_id
--------------------------------------
3 Photos 0
4 December 3
5 Bday 4
6 Celeb 5
Currently, I am stuck with this query and I am not able to make it work. The Query I am currently trying :
SELECT f.*
FROM fd_folders f
LEFT JOIN fd_folders p
ON f.folder_id = p.parent_id
WHERE f.folder_id <=6
ORDER BY f.folder_id;
But, the problem is that it is also returning the two other folders, i.e, Private and Public.
Please help.
Thank You
Best Regards