What I would like to do is construct an efficient query that brings back threads with a certain set of tags. For example, if I were to query "question, answered", the database would return threads that had both tags (and possibly more), but not those with just question or just answered. There are also constraints on threads that are being returned (such as forum ID, and limit).
I think I need a join to do this, but I'm not exactly sure how. It's easy enough doing a subquery for one tag:
Code: Select all
SELECT * FROM `threads` WHERE `forum` = $forum_id AND EXISTS (SELECT * FROM `threadtags` WHERE `threadtags`.`thread` = `threads`.`id` AND `tag` = $tag) LIMIT 10