Ok, so I’ve got this table called exp_category_posts that’s in charge of storing the relationships between an entry_id and all it’s associated cat_ids, as rows in the table (one column for entry_id, one column for cat_id)
For a search plugin I’m writing, a user can search by categories and I want to return all matching entries.
If I try to SELECT entry_id FROM exp_category_posts WHERE cat_id = 1 AND cat_id = 2 I expect to get a set of entries that are assigned to both cats but instead I get an empty results set everytime. Using OR does not give the results set I want.
The only way I’ve found to get the results I want is to break it into steps:
Step 1: Do a query for each cat_id.
Step 2: Save the result set as an array.
Step 3: Use a few in_array() calls to filter out the entries that aren’t in all of the result sets.
While this method works, it’s pretty inefficient. Is there any way I can get the results I want with a single query instead of doing it this way?
1 Answer