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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:01:56+00:00 2026-05-27T12:01:56+00:00

Failing to find a solution to this question I was wondering if there was

  • 0

Failing to find a solution to this question I was wondering if there was a better way of storing data for this problem.

That db structure allows items to be stored in multiple categories, but doesn’t allow easy access to the parent category hierarchy.

What I would like is to have a category relationship such as:

Books 
  > Novels
    > Paperbacks
    > Hardbacks

And have an item stored against Paperbacks for instance that would also show up in Novels and Books. So the ‘categories’ actually work more like filters than actual categories.

  • 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-27T12:01:57+00:00Added an answer on May 27, 2026 at 12:01 pm

    First of all you need to design your category table with usage of Nested Set architecture. With usage of Nested Sets you will easily select whole branch of categories, and then you will be able to select products for these categories.

    So the first table will be:

    CREATE TABLE categories (
      id int unsigned NOT NULL auto_increment,
      name varchar(255) NOT NULL,
      left int unsigned NOT NULL,
      right int unsigned NOT NULL,
      PRIMARY KEY (id)
    );
    

    The second table will be:

    CREATE TABLE products (
      id int unsigned NOT NULL auto_increment,
      name varchar(255) NOT NULL,
      PRIMARY KEY (id)
    );
    

    And the third table will be:

    CREATE TABLE product_categories (
      category_id int unsigned NOT NULL,
      product_id int unsigned NOT NULL,
      PRIMARY KEY (category_id, product_id)
    );
    

    Now to select all products for whole branch of categories, you need to use query like this:

    SELECT p.*
      FROM categories AS c1
      LEFT JOIN categories AS c2 ON c1.left <= c2.left AND c2.right <= c1.right
      LEFT JOIN product_categories AS pc ON pc.category_id = c2.id
      LEFT JOIN products AS p ON pc.product_id = p.id
     WHERE c1.id = @id
    

    Nested set operations

    Add new node

    1st step: update already existed categories

    UPDATE categories 
       SET right = right + 2, left = IF(left > @right, left + 2, left) 
     WHERE right >= @right
    

    2nd step: insert new category

    INSERT INTO categories SET left = @right, right = @right + 1, name = @name
    

    Delete existing node

    1st step: delete node

    DELETE FROM categories WHERE left >= @left AND right <= @right
    

    2nd step: update else nodes

    UPDATE categories 
       SET left = IF(left > @left, left – (@right - @left + 1), left), 
           right = right – (@right - @left + 1) 
     WHERE right > @right
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't seem to find a way to do this, google is failing me! Please
This is pretty easy question, but I have been unable to find the solution
I have a fairly simple question, but I'm failing to find the solution. I
i am really trying to do this but i am failing to find a
After failing to achieve this with link_to remote, I decided to try jQuery way.
I've tried googling for a solution to this problem but haven't yet found one.
I am looking for a Javascript solution to this problem: I have an XML
I'm failing to find answer to my problem. I need to be able to
I'm sure this is easy, but I'm failing to find it. I have a
I have searched on this forum and haven't been able to find a solution

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.