<p>I have topics(id*) and tags(id*,name) and a linking table topic_tags(topicFk*,tagFk*).</p>

<p>Now I want to select every single topic, that has all of the good tags (a,b,c) but none of the bad tags (d,e,f).</p>

<p>How do I do that?</p>

Here's a solution that would work, but requires a join for every tag you require.
```
```
SELECT *
FROM topics
WHERE topic_id IN
    (SELECT topic_id
    FROM topic_tags a
    INNER JOIN topic_tags b
      on a.topic_id=b.topic_id
      and b.tag = 'b'
    INNER JOIN topic_tags c
      on b.topic_id=c.topic_d
      and c.tag = 'c'
    WHERE a.tag = 'a')
AND topic_id NOT IN
    (SELECT topic_id
    FROM topic_tags
    WHERE tag = 'd' or tag = 'e' or tag = 'f')
```
```