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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:13:07+00:00 2026-05-25T13:13:07+00:00

I have a Database with a category table using the Nested Approach in MySql.

  • 0

I have a Database with a category table using the Nested Approach in MySql. But I keep hitting a wall. The Site contains “sections” species (As this is a pet store). I already have a query that returns all the categories that have products in them for each species.

Now my problem is that I want to be able to only show categories that have products under them. So for example if I create the category food with a child adult. Then the system should not list the category under any species. But as soon as I create a Product under the adult category the system should list Food with the child Adult .

The Table is currently structured as follows:

Category:

  • id
  • top
  • name
  • lft
  • rgt
  • created (for the sake of completeness)
  • lastupdated (for the sake of completeness)

So I am able to build a full tree from the table for each species but I need the categories to only be visible once they have products in them for the species.

Sounds simple enough but for some reason I can’t get my brain around this. Any Help? What would be the best way to approach this?

Update:

The query I’m using to get the Categories:

SELECT node.id as nid, node.top as top, node.*, brands.name as brandname from
brands, foods, foodcategories, categories as node, categories as parent WHERE 
node.lft BETWEEN parent.lft AND parent.rgt AND parent.top=1 AND 
foodcategories.food=foods.id AND foodcategories.category=node.id 
AND brands.id=foods.brand AND foods.species={speciesid}

I then execute a query to get all the categories and their lft,rgt. Which I can built the tree from.

  • 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-25T13:13:07+00:00Added an answer on May 25, 2026 at 1:13 pm

    After a couple of days struggling a finally thought of a solution and implemented it.

    I can execute the following query and get the entire path to and from a category,

    (SELECT cast(group_concat(parent.id SEPARATOR '-') AS char) as catpath
                FROM categories AS node,
                        categories AS parent
                WHERE node.lft BETWEEN parent.lft AND parent.rgt
                        AND node.id=categories.id
                ORDER BY node.lft )
    

    then I thought if I could this why not include this path in the category query. So when I retrieve the categories that have products in them I can simply parse this string and match the id to a list of categories that I also retrieve.

    This is quite a bit of looping. So I cache the menu and only generate it once every 5 min.

    So that’s it! 3 Queries!

    • One for the list of species.
    • One for all the categories that have
      products in them.
    • One for all the categories that I loop to find one
      with a certain ID.

    On to the next challenge !

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

Sidebar

Related Questions

I have a database that contains a date and we are using the MaskedEditExtender
I'm using EF v1. I have following tables: CREATE TABLE category ( category_id int
I have da database with categories using nested sets. i have a function, that
I have a table PAYMENTS in MySql database: CREATE TABLE `PAYMENTS` ( `ID` BIGINT(20)
I'm really upset with Hibernate! I have a database table (mysql) that holds parent-child
I have a MySQL table set up using phpMyAdmin which you can view in
I have a table story_category in my database with corrupt entries. The next query
I have a product database with several product categories. Each category has a number
My database is being driven by my NHibernate mapping files. I have a Category
I have database with many tables. In the first table, I have a field

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.