I have a query that takes tag Ids obtained from a previous query, and selects post ids from a post_tag_map table.
$tagIds for example can be something like "3,4,23,54"
.
'SELECT post_id FROM post_tag_map WHERE tag_id IN (' . $tagIds . ') ORDER BY `post_id`;'
This query works fine for its purpose, but it returns any post_id that has any one of those tag_ids.
I'm trying to obtain only post_ids that contain ALL of those tag_ids.
For example, if I search for "news, politics, campaign", I only want to return posts that have been tagged with all three of those tags, not any post that has one of the three.
How can I alter my query?
Edit: By request, my table structure is 3 tables. One (post) with post_id and post information, one (tag) with tag_id and tag_name, and post_tag_map, just maps the two Ids together.
Well it's going to be a bit more complex
Keep with your IN thing but also group posts to find ones with exact match
WHERE tag_id IN (3,4,23,54) GROUP BY `post_id` HAVING count(1) = 4;
where 4 is a number of tags we're looking for