MySQL query help

Table Structure

category_parent_id category_child_id category_list
0 3 NULL
3 4 NULL
4 5 NULL

How can i get category_child_id whose Category_parent_id = 0 when I have category_child_id = 5 or may 4 or 3.

Deep Singh

I don’t follow what you need.

can you give an example output listing

I need Top Parent Catg ID of a child child Catg., here we have Top parent to Child like this 0 -> 3->4->5.
Now I have id Child Catg id 5 and want to fetch the Top-Catg. id 3 (Top Parent id is that whose category_parent_id = 0)


can you see the thread pls.

ok, I see what you need now.

This tutorial with example sql scripts should help you.

You are using the Adjacency List Model.

My advice, if it’s not too late, is to use The Nested Set Model instead (about 1/2 way down the page).

Imho it will make life a lot easier for you, especially since you have more than 1 level of subcategories.

Thank for your advice and reply, but I can change table structure because this is Joomla table.

ok, then the example queries in the Adjacency List section should help you.