Reputation: 5972
Say for example, I have the following two tables:
TableA { _id }
TableB { _id, TableA_id, OptionString }
Each row in TableA has one or more rows in TableB referencing it (using the TableA_id foreign key).
My query lists rows in TableA to display to the user.
OptionString can be one of two values - let's say "Option1" and "Option2".
Now I want the query to still list rows in TableA, but I want to provide the user with a filter to say "only display rows in TableA where the OptionString has either "Option1" OR "Option2" (ie. exclusive or).
I've tried using COUNT, but the problem is that a zero count displays no results (not '0' as I would want).
I'm sure I must be missing something simple with this.
Any help would be greatly appreciated.
Cheers, Dan.
Upvotes: 1
Views: 1460
Reputation: 15105
Try this, if I understand your question correctly
select a._id
from tableA a
join tableB b on a._id=b.tableA_id
where b.optionString in ('Option1','Option2')
group by a._id
having count(*)=1
Revised query based on new information
select a._id
from tableA a
join (select distinct _id,tablea_id,optionString from TableB) b
on a._id=b.tableA_id
where b.optionString in ('Option1','Option2')
group by a._id
having count(*)=1
What I am doing here is forcing distinct values from tableB
Upvotes: 3
Reputation: 75275
A naive approach (might be able to improve upon this...)
SELECT *
FROM TableA
WHERE _id IN
(SELECT TableA_id
FROM TableB
WHERE OptionString IN ('Option1', 'Option2')
)
AND _id NOT IN
(SELECT T1.TableA_id
FROM TableB T1
JOIN TableB T2 ON T1.TableA_id = T2.TableA_id
WHERE T1.optionString = 'Option1'
AND T2.optionString = 'Option2'
)
The idea is simply to select qualifying records where the Option1 OR the Option2 are found, and to exclude the records where both Option1 AND Option2 are found.
If we know that a given Optionx value can only be found once for a given item of TableA, a group by query may be applicable (see Sparky's answer).
Upvotes: 1