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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:43:22+00:00 2026-05-23T02:43:22+00:00

I have three tables that look like these: PROD Prod_ID|Desc ———— P1|Foo1 P2|Foo2 P3|Foo3

  • 0

I have three tables that look like these:

PROD

Prod_ID|Desc
------------
P1|Foo1
P2|Foo2
P3|Foo3
P4|Foo4
...

RAM

Ram_ID|Desc
------------
R1|Bar1
R2|Bar2
R3|Bar3
R4|Bar4
...

PROD_RAM

Prod_ID|Ram_ID
------------
P1|R1
P2|R2
P3|R1
P3|R2
P3|R3
P4|R3
P5|R1
P5|R2
...

Between PROD and RAM there’s a Many-To-Many relationship described by the PROD_RAM table.

Given a Ram_ID set like (R1,R3) I would like to find all the PROD that has exactly ONE or ALL of the RAM of the given set.

Given (R1,R3) should return for example P1,P4 and P5; P3 should not be returned because has R1 and R3 but also R2.

What’s the fastest query to get all the PROD that has exactly ONE or ALL of the Ram_ID of a given RAM set?

EDIT:
The PROD_RAM table could contain relationship bigger than 1->3 so, “hardcoded” checks for count = 1 OR = 2 are not a viable solution.

  • 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-23T02:43:23+00:00Added an answer on May 23, 2026 at 2:43 am

    Another solution you could try for speed would be like this

    ;WITH CANDIDATES AS (
        SELECT  pr1.Prod_ID
                , pr2.Ram_ID
        FROM    PROD_RAM pr1
                INNER JOIN PROD_RAM pr2 ON pr2.Prod_ID = pr1.Prod_ID
        WHERE   pr1.Ram_ID IN ('R1', 'R3')
    )
    SELECT  *
    FROM    CANDIDATES
    WHERE   CANDIDATES.Prod_ID NOT IN (
                SELECT  Prod_ID
                FROM    CANDIDATES
                WHERE   Ram_ID NOT IN ('R1', 'R3')
            )           
    

    or if you don’t like repeating the set conditions

    ;WITH SUBSET (Ram_ID) AS (
        SELECT  'R1'
        UNION ALL SELECT 'R3'
    )   
    , CANDIDATES AS (
        SELECT  pr1.Prod_ID
                , pr2.Ram_ID
        FROM    PROD_RAM pr1
                INNER JOIN PROD_RAM pr2 ON pr2.Prod_ID = pr1.Prod_ID
                INNER JOIN SUBSET s ON s.Ram_ID = pr1.Ram_ID    
    )
    , EXCLUDES AS (
        SELECT  Prod_ID
        FROM    CANDIDATES
                LEFT OUTER JOIN SUBSET s ON s.Ram_ID = CANDIDATES.Ram_ID
        WHERE   s.Ram_ID IS NULL
    )
    SELECT  *
    FROM    CANDIDATES
            LEFT OUTER JOIN EXCLUDES ON EXCLUDES.Prod_ID = CANDIDATES.Prod_ID
    WHERE   EXCLUDES.Prod_ID IS NULL        
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database that has two tables, these tables look like this codes
I have three tables like that: Articles IdArticle Title Content Tags IdTag TagName ContentTag
I have 3 tables that look something like this: Table1 Users: ID Name Score
I have three tables: page, attachment, page-attachment I have data like this: page ID
I'm using three mysql tables that looks like: The table for members accounts |
I have a table within another table, that look like below. What I want
I have a three different tables that I'm trying to join up correctly but
I have date range data in SQL DB table that has these three (only
I have three tables tag , page , pagetag With the data below page
I have three tables in the many-to-many format. I.e, table A, B, and AB

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.