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

  • SEARCH
  • Home
  • 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 have a table that looks like this: category category_id name category_seo_friendly_url left_id right_id
I have a Category class which looks like this: @Entity @Table(name = categories) public
My MySQL looks like this: (the name of the table is category) 'id', 'content',
I've got some data in a table that looks like so: Recipe | Category
I have a categories table that looks like this: id | name | parent
I have a table that looks somewhat like this: CREATE TABLE foobar ( id
I have a table that looks something like this: ID | Keyword | Category
I have an SQL table called "posts" that looks like this: id | category
I have a mysql table named category. the basic structure looks like this- ------
I have a model called Category which looks like this: class Category < ActiveRecord::Base

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.