I am trying to select questions which have a certain topic id. These matches are stored in a mapping table, but I cannot work out how to select questions that have two tags to them seeing as they are stored as different rows in the table! Any help, please?
SELECT questions. * , posts.post, posts.id AS post_id, posts.created, users.id AS user_id, users.username, users.rep
FROM questions
LEFT JOIN posts ON questions.id = posts.question_id
LEFT JOIN users ON questions.user_id = users.id
LEFT JOIN topic_mapping ON questions.id = topic_mapping.question_id
WHERE topic_mapping.topic_id =49
OR topic_mapping.topic_id =50
GROUP BY questions.id
LIMIT 0 , 30
To find questions that have both topics assigned, test the number of distinct topics found in a HAVING clause. This number should match the number of topics you’ve included in the WHERE clause.