Help for MYSQL query

Hello Friends,

I have a table like below

id parent_id

10 0
12 10
13 1
14 12
15 14

I have above two fields. I want to find count of all the parents of 15.
For example parents 15 are - 14,12,10. So the count is 3 here. I need to go backwards and find the parents till the last parent has 0.


I need to find 15 has 3 parents or not .

How to write a query. Please help me!


This can be done with a recursive function - which is not allowed in MySQL.
Either you can program the recursive function using whichever language you are coding in, or you can restructure the database as shown here Storing Hierarchical Data in a Database Article ยป SitePoint