Select the topmost parent from the SQL table associated with the foreign foreign key.
I have a SQL table
Region
id name parent_id
1 Pune null
2 Mumbai null
3 area1 1
4 area2 3
Here I have a boolean foreign key parent_id
so that area2
has a parent area1
and area1
has a parent Pune
. Here I want to select the topmost parent of any subarea. Ex. when searching area2
then it will give a result Pune
.
+3
source to share