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


1 answer


mysql does not support hierarchical joins (for example WITH RECURSIVE

)

You can try this solution:

Hierarchical queries in MySQL



Or manipulate recursion internally php

or whatever server-side language you are using

As you asked in the comments regarding Oracle / MSSQL: Oracle , MSSQL

0


source







All Articles