I have database table Task as below.
SELECT _id,name,parentId FROM Task;
_id name parentId
---------- -------------------- ----------
4 Software Development
5 Machine Learning
6 Programing 4
7 Build System 4
8 version control 4
9 Android App Developm 4
10 Udacity Cource 5
11 Mathematics 5
12 skLearn docs 5
13 problem solving 6
14 breakdown 13
15 language 6
16 c 15
17 c++ 15
18 java 15
19 kotlin 15
20 gradle 7
21 bazel 7
22 git 8
23 svn 8
There are all tasks and their sub tasks in one table relating with respective parent task using _id(primary key) and parentId.
e.g. task name 'java' has _id = 18 and parentId = 15 means 'java' is sub task of _id = 15 which is 'language'.
Again 'language' has _id = 15 and parentId = 6 means 'language' is sub task of _id = 6 which is 'Programing'.
Same 'Programing' is sub task of 'Software development'.
And 'Software development' is sub task of null.
so I require one query which give output like below for input _id = 18(i.e. 'java') that is list of parent,parent of parent tasks... to the top for a sub task.
_id name parentId 4 Software Development null 6 Programing 4 15 language 6 18 java 15
Currently I am able to take this output using 4 query in a loop.
SELECT _id,name,parentId FROM task WHERE _id = 18
in next iteration _id would be value of parentId from output of above query
which is time consuming so can we have better solution for this.