Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2005
    Denver, CO
    Thanked 0 Times in 0 Posts

    Issue querying a category and subcategory

    I have a table in a database (store_category) that lists categories and subcategories. The subcategories have a field with the parent category's id in it. So something like:

    id | name | parent
    1 Dog   0
    2 Puppy 1
    3 Cat   0
    4 Kitty 3
    I then have a products table and in that table, each row has a field for the id of the category it's in.

    The problem I'm having is selecting something like all of the products that are in both the Dog category and it's subcategories (in this case, Puppy). I can select a single category (parent or sub) just fine, but it's if I select only the parent and want to also include the products in it's subcategory that I run in to issues.
    Josh P.

  • #2
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    St. Catharines, Ontario Canada
    Thanked 148 Times in 139 Posts
    this article explains the use of the adjacency list method quite nicely. there is also a link to Joe Celko's nested set method as well as Modified Preorder Tree Traversal.

    A great resource.


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts