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 138373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:15:14+00:00 2026-05-11T07:15:14+00:00

I am not sure if this is possible in mySQL. Here are my tables:-

  • 0

I am not sure if this is possible in mySQL. Here are my tables:-

Categories table:

  • id
  • name
  • parent_id (which points to Categories.id)

I use the above table to map all the categories and sub-categories.

Products table:

  • id
  • name
  • category_id

The category_id in the Products table points to the sub-category id in which it belongs.

e.g. If I have Toys > Educational > ABC where ABC is product, Toys is Category and Educational is sub Category, then ABC will have category_id as 2.

Now the problem is that I want to use a SQL query to display all the products (in all the sub-categories and their sub-categories.. n level) for a particular category.

e.g.:

select * from categories,products where  category.name = 'Toys' and .... 

The above query should display the products from Educational also and all other sub categories and their subcategories.

Is this possible using a mySQL query? If not what options do I have? I would like to avoid PHP recursion.

Update: Basically I want to display the top 10 products in the main category which I will be doing by adding a hits column to products table.

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T07:15:14+00:00Added an answer on May 11, 2026 at 7:15 am

    What I’ve done in previous projects where I’ve needed to do the same thing, I added two new columns.

    • i_depth: int value of how deep the category is
    • nvc_breadcrumb: complete path of the category in a breadcrumb type of format

    And then I added a trigger to the table that houses the category information to do the following (all three updates are in the same trigger)…

    -- Reset all branches UPDATE t_org_branches     SET nvc_breadcrumb = NULL,     i_depth = NULL  -- Update the root branches first UPDATE t_org_branches      SET nvc_breadcrumb = '/',          i_depth = 0      WHERE guid_branch_parent_id IS NULL  -- Update the child branches on a loop WHILE EXISTS (SELECT * FROM t_branches WHERE i_depth IS NULL)      UPDATE tobA          SET tobA.i_depth = tobB.i_depth + 1,              tobA.nvc_breadcrumb = tobB.nvc_breadcrumb + Ltrim(tobA.guid_branch_parent_id) + '/'          FROM t_org_branches AS tobA             INNER JOIN t_org_branches AS tobB ON (tobA.guid_branch_parent_id = tobB.guid_branch_id)          WHERE tobB.i_depth >= 0              AND tobB.nvc_breadcrumb IS NOT NULL              AND tobA.i_depth IS NULL 

    And then just do a join with your products table on the category ID and do a ‘LIKE ‘%/[CATEGORYID]/%’ ‘. Keep in mind that this was done in MS SQL, but it should be easy enough to translate into a MySQL version.

    It might just be compatible enough for a cut and paste (after table and column name change).


    Expansion of explanation…

    t_categories (as it stands now)…

    Cat Parent  CategoryName 1   NULL    MyStore 2   1       Electronics 3   1       Clothing 4   1       Books 5   2       Televisions 6   2       Stereos 7   5       Plasma 8   5       LCD 

    t_categories (after modification)…

    Cat  Parent  CategoryName   Depth   Breadcrumb 1   NULL    MyStore         NULL    NULL     2   1       Electronics     NULL    NULL 3   1       Clothing        NULL    NULL 4   1       Books           NULL    NULL 5   2       Televisions     NULL    NULL 6   2       Stereos         NULL    NULL 7   5       Plasma          NULL    NULL 8   5       LCD             NULL    NULL 

    t_categories (after use of the script I gave)

    Cat  Parent  CategoryName   Depth   Breadcrumb 1   NULL    MyStore         0       /    2   1       Electronics     1       /1/ 3   1       Clothing        1       /1/ 4   1       Books           1       /1/ 5   2       Televisions     2       /1/2/ 6   2       Stereos         2       /1/2/ 7   5       LCD             3       /1/2/5/ 8   7       Samsung         4       /1/2/5/7/ 

    t_products (as you have it now, no modifications)…

    ID   Cat Name 1   8   Samsung LNT5271F 2   7   LCD TV mount, up to 36' 3   7   LCD TV mount, up to 52' 4   5   HDMI Cable, 6ft 

    Join categories and products (where categories is C, products is P)

    C.Cat Parent CategoryName   Depth   Breadcrumb  ID   p.Cat  Name 1    NULL   MyStore         0       /           NULL NULL   NULL 2    1      Electronics     1       /1/         NULL NULL   NULL 3    1      Clothing        1       /1/         NULL NULL   NULL 4    1      Books           1       /1/         NULL NULL   NULL 5    2      Televisions     2       /1/2/       4    5      HDMI Cable, 6ft 6    2      Stereos         2       /1/2/       NULL NULL   NULL 7    5      LCD             3       /1/2/5/     2    7      LCD TV mount, up to 36' 7    5      LCD             3       /1/2/5/     3    7      LCD TV mount, up to 52' 8    7      Samsung         4       /1/2/5/7/   1    8      Samsung LNT5271F 

    Now assuming that the products table was more complete so that there is stuff in each category and no NULLs, you could do a ‘Breadcrumb LIKE ‘%/5/%” to get the last three items of the last table I provided. Notice that it includes the direct items and children of the category (like the Samsung tv). If you want ONLY the specific category items, just do a ‘c.cat = 5’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure if this is possible in one mysql query so I might
Not sure if this is possible: I want to use datatables to manipulate my
I am not sure if this is possible but here goes. I am using
Not sure this is possible, but looking to write a script that would return
I'm not sure if this possible but probably is fairly simple. I've made a
I'm not sure this is possible, but in ruby, you can dynamically call a
I'm not sure this is possible, but is there a syntax to be used
Not sure if this is possible. I want to grab the latest X stories
Not sure if this is possible in one Makefile alone, but I was hoping
I'm not sure this is even possible but I know if it is, the

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.