Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 415311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:22:07+00:00 2026-05-12T18:22:07+00:00

The category table looks like somewhat as below: id — name — parent_id 1

  • 0

The category table looks like somewhat as below:

id -- name -- parent_id
1  -- Men    -- 0
2  -- Women  -- 0
3  -- Shirts -- 1
4  -- Half-sleeve -- 3
5  -- Full-sleeve -- 3

Relationship table:

Product_id -- Category Id
1          -- 2
2          -- 2
3          -- 4 ....

I can retrieve the number of products in any one category and its immediate sub categories with ease with ease. But if there are more than 2 levels things get messy.

So my question is How do I get the number of all of the products in Men and its sub categories. Or Shirts and its subcategories?

Any ideas, Thanks.

UPDATE:

I know there is Nested Set Model but I am not in position to change the structure to that now.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T18:22:07+00:00Added an answer on May 12, 2026 at 6:22 pm

    If it is possible I would check out Managing Hierarchical Data in MySQL.

    It is hard to get your head around at first, but it makes tasks like this much easier.

    If you can’t do this, you will have to do a recursive function, e.g.:

    $prods = 0;
    function getProdsInCat($cat)
    {
        global $prods;
    
        $prods += mysql_result(mysql_query(SELECT COUNT(`Product_id`) FROM `prod_to_cat` WHERE `Category Id` = '".$cat."'),0);
    
    
        $moreCats = mysql_query("SELECT `cat_id` FROM `cats` WHERE `parent_id` = '".$cat."'");
        while($cats = mysql_fetch_assoc($moreCats)
        {
            getProdsInCat($cats['cat_id']);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to create a table : products which looks like (these are columns
I am working with a legacy database schema that looks like this: product_table table
i have a table called category in which i have main category ids and
table data of 2 columns category and subcategory i want to get a collection
I have a products table that contains a FK for a category, the Categories
I have the table (Product_Id, category priority, atribute1, atribute2...) in MS Access, and I
I have a categories table Category [ID, Title] and a Parent/Child table CategoryParent [ID,CategoryID,ParentCategoryID]
Consider a table with 3 columns: ID (unique, taken from an Oracle sequence), CATEGORY
I have a table story_category in my database with corrupt entries. The next query
I have a numerical field called category_id in my table. I want to do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.