• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9130
  • Last Modified:

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.
1 Solution
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now