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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:18:21+00:00 2026-05-10T23:18:21+00:00

I have a set of data that models a hierarchy of categories. A root

  • 0

I have a set of data that models a hierarchy of categories. A root category contains a set of top-level categories. Each top-level category contains a set of sub-categories.

Each sub category has a set of organizations. A given organization can appear in multiple sub categories.

The leaf nodes of this hierarchy are organizations. An organization can potentially appear in multiple sub-categories.

The data is stored in three SQL tables:

organizations organization_id organization_name 1               Org A 2               Org B 3               Org C 4               Org D 5               Org E 6               Org F  categories category_id parent_id category_name 0           NULL      Top Level Category 1           0         First Category 2           0         Second Category 3           1         Sub Category A 4           1         Sub Category B 5           1         Sub Category C 6           2         Sub Category D  organizations_categories -- Maps organizations to sub_categories organization_id category_id 1               3 2               3 2               6 3               4 4               4 5               4 6               5 6               4 7               6 8               6 

I would like to be able to select a list of all unique organizations under a given category or sub-category.

The way I’m doing it right now involves first figuring out which sub categories have been requested and then looping through each sub_category in code and performing a select to get all organizations mapped to that category. The results of each select are appended to an array. This array contains duplicates whenever an organization appears in multiple sub categories.

I would love to replace this kludge with a query that can efficiently select a list of distinct organizations given an id of one of the categories in the hierarchy.

I am devloping this solution using PHP and MySQL.

Thanks for your time and suggestions.

  • 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. 2026-05-10T23:18:22+00:00Added an answer on May 10, 2026 at 11:18 pm

    Assuming that your hierarchy is always exactly 3 levels deep:

    SELECT DISTINCT      O.organization_id,      O.organization_name FROM      Categories CAT INNER JOIN Categories SUB ON      SUB.parent_id = CAT.category_id INNER JOIN Category_Organizations CO ON      CO.category_id = SUB.category_id INNER JOIN Organizations O ON      O.organization_id = CO.organization_id WHERE      CAT.category_id = @category_id 

    You can modify that by one level to allow you to pass a sub category id. If you don’t know at the time whether or not you have a category id or a sub category id then you can do the following:

    SELECT DISTINCT      O.organization_id,      O.organization_name FROM      Categories CAT LEFT OUTER JOIN Categories SUB ON      SUB.parent_id = CAT.category_id INNER JOIN Category_Organizations CO ON      CO.category_id IN (CAT.category_id, SUB.category_id) INNER JOIN Organizations O ON      O.organization_id = CO.organization_id WHERE      CAT.category_id = @category_id 

    If your hierarchy may have an unknown number of levels (or you think it might in the future) then check out Joe Celko’s Trees and Hierarchies in SQL for Smarties for alternative ways to model a hierarchy. It’s probably a good idea to do that anyway.

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

Sidebar

Ask A Question

Stats

  • Questions 69k
  • Answers 69k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer if you have a DateTime column in your table, it's… May 11, 2026 at 12:42 pm
  • added an answer Code would look something like this: public class HomeController :… May 11, 2026 at 12:42 pm
  • added an answer From Enumerable.Cast()'s remarks: This method is implemented by using deferred… May 11, 2026 at 12:42 pm

Related Questions

I have a set of data that needs to be displayed as a crosstab
I have a costumer showing Notepad with a large set of data that looks
I am working with a set of data that I have converted to a
I have a set of data protected by 16bit checksums that I need to
We are in evaluating technologies that we'll use to store data that we gather
I have a set of objects in a hierachy. There's a top root node
I am building a hierarchy structure from scratch and I am trying to determine
As described in another question , I have a set of Model objects and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.