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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:17:22+00:00 2026-05-18T05:17:22+00:00

I am trying to write a query that looks through all combo_items and only

  • 0

I am trying to write a query that looks through all combo_items and only returns the ones where all sub_items that it references have Active=1.

I think I should be able to count how many sub_items there are in a combo_item total and then compare it to how many are Active, but I am failing pretty hard at figuring out how to do that…

My table definitions:


CREATE TABLE `combo_items` (
  `c_id` int(11) NOT NULL,
  `Label` varchar(20) NOT NULL,
  PRIMARY KEY (`c_id`)
)

CREATE TABLE `sub_items` (
  `s_id` int(11) NOT NULL,
  `Label` varchar(20) NOT NULL,
  `Active` int(1) NOT NULL,
  PRIMARY KEY (`s_id`)
)

CREATE TABLE `combo_refs` (
  `r_id` int(11) NOT NULL,
  `c_id` int(11) NOT NULL,
  `s_id` int(11) NOT NULL,
  PRIMARY KEY (`r_id`)
)

So for each combo_item, there is at least 2 rows in the combo_refs table linking to the multiple sub_items. My brain is about to make bigbadaboom 🙁

  • 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-18T05:17:23+00:00Added an answer on May 18, 2026 at 5:17 am

    I would just join the three tables usually and then combo-item-wise sum up the total number of sub-items and the number of active sub-items:

    SELECT ci.c_id, ci.Label, SUM(1) AS total_sub_items, SUM(si.Active) AS active_sub_items
    FROM combo_items AS ci
    INNER JOIN combo_refs AS cr ON cr.c_id = ci.c_id
    INNER JOIN sub_items AS si ON si.s_id = cr.s_id
    GROUP BY ci.c_id
    

    Of course, instead of using SUM(1) you could just say COUNT(ci.c_id), but I wanted an analog of SUM(si.Active).

    The approach proposed assumes Active to be 1 (active) or 0 (not active).

    To get only those combo-items whose all sub-items are active, just add WHERE si.Active = 1. You could then reject the SUM stuff anyway. Depends on what you are looking for actually:

    SELECT ci.c_id, ci.Label
    FROM combo_items AS ci
    INNER JOIN combo_refs AS cr ON cr.c_id = ci.c_id
    INNER JOIN sub_items AS si ON si.s_id = cr.s_id
    WHERE si.Active = 1
    GROUP BY ci.c_id
    

    By the way, INNER JOIN ensures that there is at least one sub-item per combo-item at all.

    (I have not tested it.)

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

Sidebar

Related Questions

I am trying to use Zend_Db_Select to write a select query that looks somewhat
I'm trying to write a query that takes a criterion and returns a count
I'm trying to write a query that will pull back the two most recent
I'm trying to write a query that extracts and transforms data from a table
I'm trying to write a query that updates rows in a table if a
I am trying to write a MySQL query that retrieves one record from table
I'm trying write a query to find records which don't have a matching record
Im trying to write a query that will tell me how much time a
I'm trying to write a query that gives me a percentage (i.e. something like
I am trying to write a query that will capture calls completed within 24

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.