UPDATE 2: I posted a full script that contains both working solutions in a related post.
UPDATE: Hans developed a query that works. See the comments. It works even if my group is (Female + French), but my Person is (Female + French + Politician) – e.g., extra categories not part of the group are ignored.
Thanks for all the help!
********************************
I’ve been working for a bit on a (possibly) hard SQL query and thought I would write out my problem here, in the hopes that (a) I’ll solve it, and (b) I’ll make the solution available to others via the magic of Teh Google.
Here’s the problem: I have a concept of “category”. Example categories: Man, Woman, Movie Star, Politician, American, French.
I have a concept of “person”. Persons can belong to one or more categories.
I have a concept of “group”. A Group is merely a listing of one or more categories. For example, “Male Movie Stars” is a Group (Man + Movie Star). “Female French Politicians” is a group (Woman + Politician + French).
What I want to do in my query is this: Given a Group, find all Persons who belong to that Group.
pseudo-code query:
select * from Person p
join PersonCategory pc on (p.personid = pc.personid)
join GroupCategory gc ON (pc.categoryid = gc.categoryid)
where gc.groupid = @groupid
However, normal JOIN syntax using the IN operator does an “OR”-type operation, when what I really want is an “AND”-type operation. Using the “Female French Politicians” example from above, this query will return French OR Woman OR Politician. Not what I want.
Thinking…









