Prasad Rajapaksha
Prasad Rajapaksha

Reputation: 6190

MySQL find super category of a given sub category

I have got a database created by someone for a development. In that database I have a table like this. This is a modal of that, If parentID is 0 those are super categories.

catID cateName parentID

1                   A                   0

2                   B                   0

3                   AA                 1

4                   BB                 2

5                  AAA                3

6                  BBB                4

When navigating through this I can get A-> AA-> AAA

The immediate upper level of 'AAA' is 'AA' and the immediate upper level of 'AA' is 'A'. So at a glance the super category of 'AAA' is 'A'. I want to obtain the super category (Which has parent category as '0') for any sub category. This subcategory flow can be unlimited. Appreciate if someone can help me for constructing a mySQL query to obtain the super category of given sub category.

Thank you.

Upvotes: 2

Views: 1084

Answers (1)

Omesh
Omesh

Reputation: 29091

As per my knowledge it is not possible to get root parent for a child at n`th level in MySQL using a single query if data is stored in this format as MySQL does not support recursive queries.

visit http://scvinodkumar.wordpress.com/2011/01/30/managing-hierarchical-data-in-mysql/

There was a similar discussion to this that might be helpful in solving this problem.

Upvotes: 1

Related Questions