Let\'s say you have the following table:
items(item_id, item_parent)
... and it is a self-referencing table - item_parent r
There is a good tech article on the mysql website about hierarchical data in MySql: Managing Hierarchical Data in MySQL - you can find a few detailed solutions with pro and cons there.
Especially the part about "The Nested Set Model" and "Finding the Depth of the Nodes" should be of interest for you.