title: MySQL SELECT query on junction table with multiple tables
body: <p>I have four tables, assuming they have only an id as a column each.</p>
  
  <ul>
  <li><code>listings</code> </li>
  <li><code>feature</code></li>
  <li><code>location</code></li>
  <li><code>l_f_location</code> as a junction table with FKs on <code>listingId</code>, <code>featureId</code>, <code>locationId</code></li>
  </ul>
  
  <p>I try to query the <code>l_f_location</code> table for a given set of <code>locationIds</code> (logical AND match) and want as a result only those listings which match ALL the <code>locationIds</code>, e.g. (7 AND 9 AND 10).</p>
  
  <p><code>l_f_location</code> looks like</p>
  
  <pre><code>listingId featureId locationId
  10        5         7
  10        7         7
  10        8         9
  11        4         7
  11        8         9
  11        9         10
  11        12        14
  </code></pre>
  
  <p>The goal is to retrieve only <code>listingId</code> 11 in this case - matching the set of <code>locationIds</code> 7, 9 and 10.</p>
  
  <p>I tried the following query</p>
  
  <p>"<code>SELECT id, COUNT(*) as num FROM l_f_locations WHERE locationId IN ( 7, 9, 10) GROUP by listingId, locationId HAVING num = 3</code>" </p>
  
  <p>But that gives wrong values of count as the grouping kicks in.</p>
  
  <p>A similar query works perfectly on a simpler junction table e.g. only l_location with e.g. "<code>SELECT id, COUNT(*) as num FROM l_location WHERE locationId IN ( 7, 9, 10) GROUP by listingId HAVING num = 3</code>".</p>
  
  <p>Rgds,
  P.</p>