title: MYSQL PDO lat lon searching with multiple in clause
body: <p>I cannot rack my brain around how to construct this sql query.</p>
  
  <p>I have 3 tables.</p>
  
  <p><strong>Users</strong> table</p>
  
  <ol>
  <li>user_id </li>
  <li>name</li>
  <li>picture
  etc</li>
  </ol>
  
  <p><strong>tag_ref</strong> table</p>
  
  <ol start="4">
  <li>user_id</li>
  <li>tag_id</li>
  </ol>
  
  <p><strong>geolocation</strong> table</p>
  
  <ol start="6">
  <li>user_id</li>
  <li>geolat</li>
  <li>geolon</li>
  </ol>
  
  <p>What I need to be able to do is find users based on location and what tags they have associated with them. I have both queries working seperately.</p>
  
  <p>This is what I have used for the location query:</p>
  
  <pre><code>$sql_search_people = "SELECT user_id, ( 3959 * acos( cos( radians(?) ) * cos( radians( geolat ) ) * cos( radians( geolon ) - radians(?) ) + sin( radians(?) ) * sin( radians( geolat ) ) ) ) AS distance FROM geolocation 
          HAVING distance &lt; '25'";
  </code></pre>
  
  <p>I then need to filter results by tag_id from tag_ref table. Users can search by multiple tags. I have done this seperatley here:</p>
  
  <pre><code>$sql_search_people = "SELECT b.user_id, b.name, b.picture, b.tagline, b.genres FROM tag_ref AS a LEFT JOIN users AS b ON a.user_id = b.user_id WHERE a.tag_id IN ($in) GROUP BY a.user_id";
  </code></pre>
  
  <p>Now I just need to figure out how to combine the two together into one query. Which I cannot figure out! I've tried joins and sub queries but am really struggling to understand these.</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>** UPDATE **</p>
  
  <p>After playing about a bit more i managed to get this to work:</p>
  
  <pre><code>    $sql_search_people = "SELECT a.user_id, b.user_id, c.user_id, c.tag_id, b.name, ( 3959 * acos( cos( radians(?) ) * cos( radians( geolat ) ) * cos( radians( geolon ) - radians(?) ) + sin( radians(?) ) * sin( radians( geolat ) ) ) ) AS distance FROM geolocation AS a RIGHT JOIN tag_ref AS c ON a.user_id = c.user_id RIGHT JOIN users AS b ON a.user_id = b.user_id WHERE c.tag_id IN ($in) HAVING distance &lt; '25' ";
  </code></pre>
  
  <p>But of course it is duplicating results where a user has more than one tag. When I add a group by to group by user_id the query fails?</p>