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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:23:55+00:00 2026-05-30T20:23:55+00:00

I have two tables in MySQL that I’m comparing with the following attributes: tbl_fac

  • 0

I have two tables in MySQL that I’m comparing with the following attributes:

tbl_fac : facility_id, chemical_id, criteria
             10      , 25         , 50
             10      , 26         , 60
             10      , 27         , 60
             11      , 25         , 30
             11      , 27         , 31 
              etc...

tbl_samp: sample_id, chemical_id, result
            5     ,    25         , 51
            5     ,    26         , 61
            6     ,    25         , 51
            6     ,    26         , 61
            6     ,    27         , 500

              etc.... 

These tables are joined by chemical_id (many-to-many—- ugh), and there are several thousand facility_id’s, and several hundred chemical_id’s for each facility_id. There are also several thousand sample_id’s, each with several hundred chemical_id’s for each sample_id. All-in-all, there are around 500,000 records in tbl_fac, and 1,000,000+ records in tbl_samp.

I’m trying to extract three groups of sample_id’s from this dataset:

Group 1: any sample_id where tbl_samp.result > tbl_fac.criteria (i.e., result exceeds criteria)

Group 2: any sample_id where tbl_samp.result < tbl_fac.criteria, AND all tbl_fac.chemical_id’s are present for that sample_id (i.e., result is less than criteria, and everything is there)

Group 3: any sample_id where tbl_samp.result < tbl_fac.criteria, BUT one or more tbl_fac.chemical_id’s are missing in the sample_id (i.e., result is less than criteria, but something is missing)

Here’s the Question: How do I get all three Groups efficiently in one query?

I’ve tried:

select * 
from tbl_fac 
left join tbl_samp 
    on tbl_fac.chemical_id = tbl_samp.chemical_id

But this only yields values that are missing for the entire dataset (not the individual samples). I have a hackish query working that uses a third table to join tbl_fac and tbl_samp, but it is so ugly I’m actually embarrassed to post it….

As always, many thanks in advance for your thoughts on this one!

Cheers,

Josh

EDIT: Ideally, I would like the sample_id and Group returned — with just one Group per sample ID (my knowledge of the data indicates that they will always fall into one of the three categories above).

  • 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-30T20:23:57+00:00Added an answer on May 30, 2026 at 8:23 pm

    This answer makes the assumption that there is a unique constraint on facility_id and chemical_id in tbl_fac and a unique constraint on sample_id and chemical_id in tbl_samp. What I did was build up the query one step at a time. Whether this is efficient remains to be seen.

    Group 1: any sample_id where tbl_samp.result > tbl_fac.criteria (i.e., result exceeds criteria)

    SELECT tbl_samp.sample_id,
           'ResultsGreaterThanCriteria' AS samplegroup
    FROM   tbl_fac
           INNER JOIN tbl_samp
             ON tbl_fac.chemical_id = tbl_samp.chemical_id
    WHERE  tbl_samp.result > tbl_fac.criteria
    GROUP  BY tbl_samp.sample_id
    

    Group 2: any sample_id where tbl_samp.result < tbl_fac.criteria, AND all tbl_fac.chemical_id’s are present for that sample_id (i.e., result is less than criteria, and everything is there)

    SELECT tbl_samp.sample_id,
           'ResultLessThanCriteriaAndAllChems' AS samplegroup
    FROM   tbl_fac
           INNER JOIN tbl_samp
             ON tbl_fac.chemical_id = tbl_samp.chemical_id
    WHERE  tbl_samp.result < tbl_fac.criteria
           AND NOT EXISTS (SELECT *
                           FROM   tbl_fac tf
                                  LEFT JOIN tbl_samp ts
                                    ON tf.chemical_id = ts.chemical_id
                           WHERE  ts.chemical_id IS NULL
                                  AND tbl_samp.sample_id = ts.sample_id)
    GROUP  BY tbl_samp.sample_id
    

    Group 3: any sample_id where tbl_samp.result < tbl_fac.criteria, BUT one or more tbl_fac.chemical_id’s are missing in the sample_id (i.e., result is less than criteria, but something is missing)

    SELECT tbl_samp.sample_id,
           'ResultsLessThanCriteriaWithMissingChems' AS samplegroup
    FROM   tbl_fac
           INNER JOIN tbl_samp
             ON tbl_fac.chemical_id = tbl_samp.chemical_id
    WHERE  tbl_samp.result < tbl_fac.criteria
           AND EXISTS (SELECT *
                       FROM   tbl_fac tf
                              LEFT JOIN tbl_samp ts
                                ON tf.chemical_id = ts.chemical_id
                       WHERE  ts.chemical_id IS NULL
                              AND tbl_samp.sample_id = ts.sample_id)
    GROUP  BY tbl_samp.sample_id 
    

    And finally, you union all three queries together and get:

    SELECT tbl_samp.sample_id,
           'ResultsGreaterThanCriteria' AS samplegroup
    FROM   tbl_fac
           INNER JOIN tbl_samp
             ON tbl_fac.chemical_id = tbl_samp.chemical_id
    WHERE  tbl_samp.result > tbl_fac.criteria
    GROUP  BY tbl_samp.sample_id
    UNION ALL
    SELECT tbl_samp.sample_id,
           'ResultLessThanCriteriaAndAllChems' AS samplegroup
    FROM   tbl_fac
           INNER JOIN tbl_samp
             ON tbl_fac.chemical_id = tbl_samp.chemical_id
    WHERE  tbl_samp.result < tbl_fac.criteria
           AND NOT EXISTS (SELECT *
                           FROM   tbl_fac tf
                                  LEFT JOIN tbl_samp ts
                                    ON tf.chemical_id = ts.chemical_id
                           WHERE  ts.chemical_id IS NULL
                                  AND tbl_samp.sample_id = ts.sample_id)
    GROUP  BY tbl_samp.sample_id
    UNION ALL
    SELECT tbl_samp.sample_id,
           'ResultsLessThanCriteriaWithMissingChems' AS samplegroup
    FROM   tbl_fac
           INNER JOIN tbl_samp
             ON tbl_fac.chemical_id = tbl_samp.chemical_id
    WHERE  tbl_samp.result < tbl_fac.criteria
           AND EXISTS (SELECT *
                       FROM   tbl_fac tf
                              LEFT JOIN tbl_samp ts
                                ON tf.chemical_id = ts.chemical_id
                       WHERE  ts.chemical_id IS NULL
                              AND tbl_samp.sample_id = ts.sample_id)
    GROUP  BY tbl_samp.sample_id 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two mysql tables that have the following structure: Table 1: ---ID---------NAME------- ---1-----
I was thinking that I would have two tables for mysql. One for storing
I have two MySQL tables: attributes (attributeid, name) productsattributes (productid, attributeid, displayvalue) The required
If I have two tables in mysql that have similar columns... TABLEA id name
If I have two tables in a MySQL database that both have a column
I'm trying to use natural join to join two tables in mySQL that have
I have two MySQL (MyIsAm) tables that represent letting units and bookings: LettingUnits (
I have two MySQL database tables that are meant to hold data for eshop
I have two MySQL tables describing data that can be extended into subclasses, one
Hello I have two mysql tables one that holds username and password second that

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.