No, not in MySQL, at least. This is one of the biggest limitations of the Model List of adjacent regions .
You could continue to independently join a finite number of times, but this is ugly, inconvenient, and not covering an unlimited department. You can also download all the data in your application, build a tree and find the path in the application.
Some DBMSs, such as SQL Server 2005, Postgres 8.4, and Oracle 11g, support recursive queries using common table expressions with the WITH
keyword. This feature makes it easy to write questions like this, but unfortunately MySQL does not yet support recursive queries.
You might be interested in checking out the following article, which describes an alternative model (a nested set model ) that makes recursive operations easier (maybe) in MySQL:
In addition, I also suggest checking out the following presentation by @Bill Karwin , a regular contributor to Stack Overflow:
The closure table model described in the presentation is a very important alternative to a nested set. He describes this model in more detail in his book SQL Antipatterns ( excerpt from a chapter on this subject ).
source share