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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:29:07+00:00 2026-05-23T19:29:07+00:00

I have a table structure as follows and I want to select all active

  • 0

I have a table structure as follows and I want to select
all active categories
with active products
of a particular parent ID.

So if I plug in a particular parentID, I only get relevant active subcategories of the parent that contains active products.

This is my sql (mysql) so far which works, but looks pretty nasty, and for the sake of academia I want to know if there’s a better way. It would seem to me that selecting all of the productIDs that are active to filter the results is kind of a waste, but I can’t figure a way around this, or does mysql figure out the best way to process this query?

(many-to-many upon itself)
categories
----------
categoryID
parentID
name
isActive (bool)

(linker table between categories and product)
productCategories
-----------------
productID
categoryID

products
--------
productID
name
isActive (bool)


SELECT productCategories.categoryID, categories.* FROM productCategories
                LEFT JOIN categories ON
                    productCategories.categoryID = categories.categoryID
                WHERE 
                    productCategories.categoryID IN 
                    (SELECT categoryID FROM categories WHERE parentID = {$parentID} AND isActive = 1)
                    AND
                    productCategories.productID IN
                    (SELECT productID FROM products WHERE isActive = 1)
                GROUP BY productCategories.categoryID
  • 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-23T19:29:08+00:00Added an answer on May 23, 2026 at 7:29 pm

    An alternative layout could be as follows…

    SELECT
      *
    FROM
    (
      SELECT
        productCategories.categoryID
      FROM
        productCategories
      INNER JOIN
        categories
          ON categories.categoryID = productCategories.parentID
      INNER JOIN
        products
          ON products.productID    = productCategories.productID
      WHERE 
        categories.parentID = {$parentID}
        AND categories.isActive = 1
        AND products.isActive = 1
      GROUP BY
        productCategories.categoryID
    )
      AS category_map
    LEFT JOIN
      categories AS [children]
        ON category_map.categoryID = categories.categoryID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have table structure with 3 colums (column1, column2, column3) and i want to
I have a table structure with columns like this [ID] [Name] [ParentId] [ParentName] The
I have table structure as follows id productid ip hittime --------------------------------------------------------------------------- 1 5 1.1.1.1
I have the following table structure as follows: (please ignore the blackbox. was formatting
I have database table whose structure is as follows: id int name varchar(20) dob
Problem Assumption: I have a table in SQL Server, with the structure as follows;
I have a table structure that looks like: <table> <tr id=row1> <td> <div>row 1
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I have some table structure: <tr class=row-2><tr> <tr class=row-3>..<tr> <tr class=row-4>..<tr> <tr class=row-5>..<tr> <tr
I have this table structure and would like to map it using Fluent Hibernate

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.