title: Mysql join query for multiple "tags" (many-to-many relationship) that matches ALL tags?
body: <p>I am trying to query for Objects that match ALL of a given set of Tags. </p>
  
  <p>Basically I want users to be able to add on more and more Tags to filter or "narrow down" their search results, kind of like newegg.com does.</p>
  
  <p>My table structure is a table of Objects, a table of Tags, and a MANY:MANY relation table ObjectsTags. So I have a JOIN query like so:</p>
  
  <pre><code>SELECT * FROM Objects
  LEFT OUTER JOIN ObjectsTags ON (Objects.id=ObjectsTags.object_id)
  LEFT OUTER JOIN Tags ON (Tags.id=ObjectsTags.tag_id)
  </code></pre>
  
  <p>I tried using an IN clause/condition, like this:</p>
  
  <pre><code>SELECT * FROM Objects
  LEFT OUTER JOIN ObjectsTags ON (Objects.id=ObjectsTags.object_id)
  LEFT OUTER JOIN Tags ON (Tags.id=ObjectsTags.tag_id)
  WHERE Tags.name IN ('tag1','tag2')
  GROUP BY Objects.id
  </code></pre>
  
  <p>But I learned that this simulates a series of ORs, so the more tags you add to the query the MORE results you get, instead of the result set narrowing down like I was hoping.</p>
  
  <p>I also tried doing multiple LIKE WHERE conditions, ANDed together:</p>
  
  <pre><code>SELECT * FROM Objects
  LEFT OUTER JOIN ObjectsTags ON (Objects.id=ObjectsTags.object_id)
  LEFT OUTER JOIN Tags ON (Tags.id=ObjectsTags.tag_id)
  WHERE Tags.name LIKE 'tag1' 
  AND Tags.name LIKE 'tag2'
  GROUP BY Objects.id
  </code></pre>
  
  <p>But this returns no results, since when the results are grouped together the OUTER JOINed Tags.name column just contains 'tag1', and not also 'tag2'. The result row where 'tag2' matched is "hidden" by the GROUPing.</p>
  
  <p>How can I match ALL of the tags to get the "narrow down" or "drill down" effect that I am after? Thanks.</p>