This is relating to my last question mysql query with AND, OR and NOT
Instead of editing the question, I am asking a new one because the question is only part of the previous question with an alteration.
I am looking to do a mysql query that returns me all articles that have all required topics.
Article
id
....
Topic
id
....
ArticleTopics
article_id
topic_id
type
something that would effectively do:
SELECT * FROM Article LEFT JOIN ArticleTopics ON Article.id = ArticleTopics.article_id
WHERE ArticleTopics.topic_id HAS ALL (these topics)
Is this possible? What is the best approach for this?
Several of the other answers suggest using aliases on the child table for each filter clause – this may not be very efficient or scale well.
Consider:
Another advantage of this approach is that the structure of the query doesn’t need to change with the number of topics you are searching for – you could even put them in a temporary table and perform a join instead of using the ‘ IN (…)’ literal.
You’d need to try it out to see which query behaves better.