this is a table in mysql.
sub_table:
id name parent_id
1 sub 0 //it means this the sub has not any parents.
2 sub1 1 //it means the parent is sub
3 sub2 1
4 sub3 3
5 sub4 4
6 sub5 0
7 sub6 6
How can I specify an identification number and get its parent root identifier?
eg:
if id = 5 return me 1
if id = 6 will return me 6
if id = 7 will return me 6
SELECT id from table sub_table
WHILE parent_id != 0
BEGIN
...?..
END
source
share