OsakaWebbie
Programmer
I thought surely this has been answered many times, but I couldn't find it in a search.
I am re-writing in PHP/MySQL the functionality I had in MS Access, so I'm struggling not to think through queries by using "where some-field in (select some-field from such-and-such). I have a table Person with key PersonID, a table Category with key CategoryID, and a table PerCat with a key of both IDs to form a many-to-many relationship (fairly normal, I think). I want to display checkboxes for all the categories but in two groups - the ones that the person already belongs to, and the ones they don't. Knowing that I couldn't use the "where X in (select X from..." functionality, I tried the following (and several variations), but something isn't working right:
SELECT category.CategoryID, Category, PersonID FROM category LEFT JOIN percat ON Category.CategoryID=percat.CategoryID WHERE PersonID=$pid ORDER BY PersonID, Category
I thought I would get all the categories, with PersonID blank for the ones where there was no PerCat record for that PersonID (ordered by the existence of PersonID, so that I can work with the two cases in order). But it only gives me the records where the PerCat record exists. Does someone see what I am doing wrong? Or do you know a better way to work around MySQL's lack of "where X in (select X..."? Thanks!
I am re-writing in PHP/MySQL the functionality I had in MS Access, so I'm struggling not to think through queries by using "where some-field in (select some-field from such-and-such). I have a table Person with key PersonID, a table Category with key CategoryID, and a table PerCat with a key of both IDs to form a many-to-many relationship (fairly normal, I think). I want to display checkboxes for all the categories but in two groups - the ones that the person already belongs to, and the ones they don't. Knowing that I couldn't use the "where X in (select X from..." functionality, I tried the following (and several variations), but something isn't working right:
SELECT category.CategoryID, Category, PersonID FROM category LEFT JOIN percat ON Category.CategoryID=percat.CategoryID WHERE PersonID=$pid ORDER BY PersonID, Category
I thought I would get all the categories, with PersonID blank for the ones where there was no PerCat record for that PersonID (ordered by the existence of PersonID, so that I can work with the two cases in order). But it only gives me the records where the PerCat record exists. Does someone see what I am doing wrong? Or do you know a better way to work around MySQL's lack of "where X in (select X..."? Thanks!