MySQL query - dealing with parent- child relations in a table (multiple levels)


I have table [categories] - this table contains all the categories in my product catalogue.

there table has a number of fields - the 2 of importance here are the categories id (Unique)
and the parent_id

The top level categories have a blank parent - this is because they are not contained inside another category , there is only 12 of these , the remaining 5,000 categories have a parent id,

The parent id on a record say "This category(category_id) is contained inside this category(parent_id) "

What i am looking for is a well optimised query that will return all the category_id's that are inside a given category id

i.e. child categories, child child categories , child child child categories - an so on.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
for hierarchical data (in MySQL), I found this article most helpful:

you will notice that with "usual" table design, mysql lacking recursive queries, you cannot do it in 1 single query. the workaround would be to solve this with a stored procedure (for example) looping until all child records have been found...
Hi there,

As MySQL doesn't support recursive select, I always use recursive function to create my tree.
Here is my code (from one of my PHP projects):

// Returns an array with list of subcategories that belong to a given parent node (except node exceptID)
function GetSubCategList(mysqli &$aDb, $rootID = 0, $exceptID = 0){

    // makes the SQL
    $sqlBase = "select c.CategoryID, if(isnull(c.ParentID), 0, c.ParentID) as ParentID, 
                    c.Name, c.Description, p.Name as ParentName, c.Idx, c.Poza, c.Memo 
                from tbl_categories c 
                left join tbl_categories p on (c.ParentID = p.CategoryID)
                where if(c.ParentID is null, 0, c.ParentID) = $rootID
                    and c.CategoryID <> $exceptID 
                order by c.Idx, c.Name";
    $result = array();
    if ($rs = $aDb->query($sqlBase)) {
        while ($row = $rs->fetch_array(MYSQLI_ASSOC)) {
        	array_push($result, $row);

    return $result;
//  Returns an array with the whole categories tree, starting from the given parent
function MakeCategoryTree(&$aDb, $ParentNodeID, $exceptID = 0, $nLevel = 0){
    $tblRes = GetSubCategList(&$aDb, $ParentNodeID, $exceptID);
    $arrResult = array();
    // tree loop
    foreach($tblRes as $row){
        $row['Level'] = $nLevel;
        array_push($arrResult, $row);
        $arrResult = array_merge($arrResult, MakeCategoryTree(&$aDb, $row['CategoryID'], $exceptID, $nLevel) );
    return $arrResult;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.