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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:13:02+00:00 2026-06-18T00:13:02+00:00

I have a database with an Adjacency List approach to handle categorization of products

  • 0

I have a database with an Adjacency List approach to handle categorization of products in which one product may be found under many categories. Get a look at the following database layout:

cats
id    parent    title           desc
1     0         top             top level
2     1         Electronics
3     2         Gaming
4     2         Computers
5     4         Tablets
6     1         Food
7     3         Xbox

products
id        title         qty
1         ToshibaTV     5
2         I-PAD2        9
3         Laser Pen     24
4         Asus Notebook 5


cats_products
id   product_id   cat_id 
1    2            3
2    2            5
3    1            2
4    3            2
5    4            4 

In the above example I need an SQL query that is able to retrieve all the products found in Electronics category and any child category of it with any level (Xbox for example which it is not directly child of Electronics) without repeat of the product that found in more than one category like I-PAD2.

I could able to do this with the help of the application in PHP but I wonder if it is possible to do it using just pure sql in MySQL?

  • 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-06-18T00:13:03+00:00Added an answer on June 18, 2026 at 12:13 am

    What you’re trying to do is rather difficult using the Adjacency List Model. As @Mike suggested, using a Nested Set Model would make this so much easier. Or alternatively doing it via your php code would even be easier.

    However, assuming you know how many parent-child levels there could be (or you can assume there wouldn’t be more than X), you can try something like this. This would be easier to read if MySQL supported CTEs, but unfortunately, it does not. In this example, I’ve gone 4 levels deep — you can get the idea of going deeper.

    SELECT p.Id, p.Title, p.Qty
    FROM Products p
       JOIN Cats_Products cp on p.id = cp.product_id
    WHERE cp.cat_id IN (
       SELECT c.id
       FROM Cats c
       WHERE c.title = 'Electronics'
       UNION ALL
       SELECT c2.id
       FROM Cats c
          LEFT JOIN Cats c2 ON c.id = c2.parent
       WHERE c.title = 'Electronics'
       UNION ALL
       SELECT c3.id
       FROM Cats c
          LEFT JOIN Cats c2 ON c.id = c2.parent
          LEFT JOIN Cats c3 ON c2.id = c3.parent
       WHERE c.title = 'Electronics'
       UNION ALL
       SELECT c4.id
       FROM Cats c
          LEFT JOIN Cats c2 ON c.id = c2.parent
          LEFT JOIN Cats c3 ON c2.id = c3.parent
          LEFT JOIN Cats c4 ON c3.id = c4.parent
       WHERE c.title = 'Electronics'
    )
    GROUP BY p.Id, p.Title, p.Qty
    

    And here is the SQL Fiddle.

    Good luck.

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

Sidebar

Related Questions

I have an adjacency list of objects (rows loaded from SQL database with the
Currently I have database with the following associations: One Client to Many Intakes One
I have an adjacency list in a database with ID and ParentID to represent
I have database, and in one the tables I need change the values of
So far I have encountered adjacency list, nested sets and nested intervals as models
I have 3 text files each of which contains a list of 20 items.
i have database table with few text fields which i use to filter data
I have database backups taken from 2 machines(same database). ( about 75 tables) One
I have database which has two tables post: id post_name post_desc files: file_id file_name
I have database schema for an integration project in which I need to be

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.