OsakaWebbie
Programmer
I have a table called "person" with a primary key PersonID. I also have a table "category" with a primary key CategoryID, and a table "percat" (PersonID, CategoryID) to form a many-many relationship between people and categories. I want to be able to add "filters" to only show people who belong to certain categories and/or don't belong to others. There could be any number of filters, set by the user to either require membership in one or more categories, exclude members of one or more categories, or both.
This looks like a job for multiple subqueries (one subquery of percat for each filter), but my hoster is still using MySQL 4.0.18, so I have to come up with another way. The web page only shows very simple examples with either existence or absence in a second table - that doesn't address my situation, because I need to examine multiple percat records before determining whether a person record should be returned. Let's say, for example, that I want to select all person records that "belong" to categories 1 and 3, but don't belong to 2 and 4, how can I make a single query to do it? All the ideas I have come up with only look at one percat record at a time ("SELECT person.* FROM person, percat WHERE person.PersonID=percat.PersonID AND [some logic statement about percat.CategoryID]", or a join that does about the same thing), but I don't know how to meld it together. Can someone help?
This looks like a job for multiple subqueries (one subquery of percat for each filter), but my hoster is still using MySQL 4.0.18, so I have to come up with another way. The web page only shows very simple examples with either existence or absence in a second table - that doesn't address my situation, because I need to examine multiple percat records before determining whether a person record should be returned. Let's say, for example, that I want to select all person records that "belong" to categories 1 and 3, but don't belong to 2 and 4, how can I make a single query to do it? All the ideas I have come up with only look at one percat record at a time ("SELECT person.* FROM person, percat WHERE person.PersonID=percat.PersonID AND [some logic statement about percat.CategoryID]", or a join that does about the same thing), but I don't know how to meld it together. Can someone help?