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.
Try this
or
UPDATE
If you want only matching pairs
The sub-select groups by
A_IDand counts the records. The HAVING clause works like the WHERE clause but is executed after grouping. So the inner select returns onlyA_IDs corresponding to (4, 5)-pairs ofC_ID. The whole query always returns an even number of records likeEDIT
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
If the two numbers (4 and 5 in this example) are always contiguous, you can drop the
COUNT(*)=2part.(Note: accoring to one of your comments the join is on the
A_IDcolumn. I changed that in all my examples.)UPDATE by Robin
Thanks, with your help I came up with this: