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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:35:52+00:00 2026-06-15T21:35:52+00:00

I have a table A with column B_ID refering to B_ID in table B

  • 0

I have a table A with column B_ID refering to B_ID in table B
Table B has B_ID as primary key and C_ID refering to C_ID in table C

I want to select from A join B on A_ID where B.C_ID = 5 AND B.C_ID = 4

for example when I do this:

select from A join B on B.A_ID = A.A_ID

If the result is :

A_ID  |  B_ID  |  C_ID
 1    |   1    |   4
 1    |   2    |   5

Then I want the query to return both rows
But if the result is :

A_ID  |  B_ID  |  C_ID
 1    |   1    |   4

I want the query to return 0 rows.

  • 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-06-15T21:35:53+00:00Added an answer on June 15, 2026 at 9:35 pm

    Try this

    SELECT
        whatever
    FROM
        A
        INNER JOIN B
            ON A.A_ID = B.A_ID
    WHERE
        B.C_ID IN (4, 5)
    

    or

    SELECT
        whatever
    FROM
        A
        INNER JOIN B
            ON A.A_ID = B.A_ID
    WHERE
        B.C_ID = 4 OR B.C_ID = 5
    

    UPDATE

    If you want only matching pairs

    SELECT
        whatever
    FROM
        A
        INNER JOIN B
            ON A.A_ID = B.A_ID
    WHERE
        A.A_ID IN (SELECT A_ID
                   FROM B
                   WHERE C_ID IN (4, 5)
                   GROUP BY A_ID
                   HAVING COUNT(*) = 2) AND
        B.C_ID IN (4, 5)
    

    The sub-select groups by A_ID and counts the records. The HAVING clause works like the WHERE clause but is executed after grouping. So the inner select returns only A_IDs corresponding to (4, 5)-pairs of C_ID. The whole query always returns an even number of records like

    A_ID  |  B_ID  |  C_ID
     1    |   1    |   4
     1    |   2    |   5
     2    |   3    |   4
     2    |   4    |   5
    

    EDIT

    If you only want A_IDs where not only C_IDs 4 and 5 are present but where no further C_IDs exist then change the query to

    SELECT B.*
    FROM A INNER JOIN B ON A.A_ID = B.A_ID
    WHERE B.C_ID IN (4, 5) AND
          A.A_ID IN (SELECT A_ID
                     FROM B
                     GROUP BY A_ID
                     HAVING MIN(C_ID)=4 AND MAX(C_ID)=5 AND COUNT(*)=2)
    

    If the two numbers (4 and 5 in this example) are always contiguous, you can drop the COUNT(*)=2 part.

    (Note: accoring to one of your comments the join is on the A_ID column. I changed that in all my examples.)

    UPDATE by Robin

    Thanks, with your help I came up with this:

    SELECT
        *
    FROM
        A a
        INNER JOIN B
            ON a.A_ID = B.A_ID
    WHERE
        (SELECT COUNT(*) FROM B b WHERE b.A_ID = a.A_ID and C_ID IN (4, 5)) =
     (SELECT COUNT(*) FROM A aa INNER JOIN B b ON aa.A_ID = b.A_ID WHERE b.A_ID = a.A_ID)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have 3 tables A,B and C. table A has column employee_name,id table B
I have table with 3 columns A B C. I want to select *
For example I have the following tables resulting from: CREATE TABLE A (Id int,
I have two tables I want to update table b in column bcnt by
I have a table 'node' wich has a column 'nid'. This id corresponds to
Hallo, I have a join table, said tableA and tableB. tableA have a column
I have 2 tables. Table A has just one column called ID which has
I have a Inline table valued Function that i want to pass a column
Imagine I have this table: Column A | Column B | Column C ------------------------------
Assume I have a table like this: column A(int) column B(int) 1 2 2

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.