title: Get results that fall within marker radiuses from database
body: <h1>Update 16th November 2012</h1>
  
  <p>I would like to raise this question again, offering with a new bounty for a solid, good solution. It seems that only the solution (<a href="https://stackoverflow.com/a/11587424/264795"><code>shubhansh</code>'s answer</a>) does not effectively work now. I will explain why.</p>
  
  <p>Firstly, this is the live map I have with radiuses and people, the radiuses are in <strong><code>red</code></strong> and the people are in <strong><code>blue</code></strong>.</p>
  
  <p><img src="https://i.stack.imgur.com/j11MZ.jpg" alt="enter image description here"></p>
  
  <p>As you can see, there are <strong><code>two</code></strong> people in this map with <strong><code>eight</code></strong> radiuses, basically I am getting only the person which is <strong><code>Person A</code></strong>, but I am not getting <strong><code>Person B</code></strong>, I'm guessing that the SQL is not correctly picking it up which I need it to be precise and accurate from the person's radius and the marker radiuses.</p>
  
  <p>It looks like what is picked up are are inside the radiuses, not those who overlap a radius, I need it to be able to pick up any results for any radiuses that overlap each other.</p>
  
  <p>I am looking for a precise and accurate SQL than shubhansh's answer. You may read below to read how exactly I need the query to act and pick up accurate people.</p>
  
  <p>The data, <strong>PEOPLE</strong>:</p>
  
  <pre><code>+-----------+-----------+--------+
  | latitude  | longitude | radius |
  +-----------+-----------+--------+
  | 51.517395 | -0.053129 | 5.6    |
  | 51.506607 | -0.116129 | 0.7    |
  +-----------+-----------+--------+
  </code></pre>
  
  <p>Please note that <code>radius</code> is in kilometers.</p>
  
  <pre><code>+-----------+-----------+-----+
  | latitude  | longitude | km  |
  +-----------+-----------+-----+
  | 51.502117 | -0.103340 | 0.3 |
  | 51.498913 | -0.120850 | 0.7 |
  | 51.496078 | -0.108919 | 0.7 |
  | 51.496506 | -0.095873 | 0.7 |
  | 51.503399 | -0.090723 | 0.7 |
  | 51.508049 | -0.100336 | 0.7 |
  | 51.508797 | -0.112610 | 0.7 |
  | 51.505535 | -0.125227 | 0.7 |
  | 51.502331 | -0.108061 | 0.7 |
  +-----------+-----------+-----+
  </code></pre>
  
  <p>The current SQL I use:</p>
  
  <pre><code>SELECT ppl.latitude,
         ppl.longitude,
         ppl.radius
  FROM 
  (
      people ppl
  ),
  (
      SELECT latitude, longitude 
      FROM radiuses
  ) AS radius
  WHERE (POW((ppl.longitude - radius.longitude) * 111.12 * COS(ppl.latitude), 2) + POW((ppl.longitude - radius.longitude) * 111.12, 2)) &lt;= 4
  GROUP BY ppl.id
  </code></pre>
  
  <p>The data for MySQL which you can use to test your query,</p>
  
  <pre><code>INSERT INTO radiuses (id, latitude, longitude, km) VALUES ('1', '51.502117', '-0.103340', '0.3'), ('2', '51.498913', '-0.120850', '0.7'), ('3', '51.496078', '-0.108919', '0.7'), ('4', '51.496506', '-0.095873', '0.7'), ('5', '51.503399', '-0.090723', '0.7'), ('6', '51.508049', '-0.100336', '0.7'), ('7', '51.508797', '-0.112610', '0.7'), ('8', '51.505535', '-0.125227', '0.7'), ('9', '51.502331', '-0.108061', '0.7');
  
  INSERT INTO people (id, latitude, longitude, radius) VALUES ('1', '51.517395', '-0.053129', '5.6'), ('2', '51.506607', '-0.116129', '0.7');
  </code></pre>
  
  <hr>
  
  <h1>Old summary</h1>
  
  <p><strong>Note:</strong> all the latitudes and longitudes are just randomly made.</p>
  
  <p>I have a map applet which a user can place his <em>radius</em> of a lat/lng location, with a 1km radius.</p>
  
  <p>Now, there is another user that can put his <em>radiuses</em>, at any location on the map, each with 1km radius (same as the user above). </p>
  
  <p>Like this <strong>User A</strong> is red and <strong>User B</strong> is blue.</p>
  
  <p><img src="https://i.stack.imgur.com/J5QBp.png" alt="enter image description here"></p>
  
  <p>Basically <strong>User A</strong> stores his radiuses in a table that looks like this:</p>
  
  <pre><code>+-----------+---------+-----------+-----------+
  | radius_id | user_id | latitude  | longitude |
  +-----------+---------+-----------+-----------+
  |         1 |       1 | 81.802117 | -1.110035 |
  |         2 |       1 | 81.798272 | -1.144196 |
  |         3 |       1 | 81.726782 | -1.135919 |
  +-----------+---------+-----------+-----------+
  </code></pre>
  
  <p>And <strong>User B</strong> stores his radius in another table that looks like this - (note: they can only store 1 coordinates per account):</p>
  
  <pre><code>+---------+-----------+-----------+
  | user_id | latitude  | longitude |
  +---------+-----------+-----------+
  |       6 | 81.444126 | -1.244910 |
  +---------+-----------+-----------+
  </code></pre>
  
  <p>I want to be able to pick up those users that fall within the defined radiuses, even if the radius circles are touching, in the map picture. Only marker <code>C</code> would be able to pick up the single radius, when <code>A</code> and <code>B</code> do not.</p>
  
  <p>I'm sure this is possible, but I do not know how to come up with this kind of system in MySQL.</p>
  
  <p>I found this on the Google Developers site it's close but not just what it performs I need.</p>
  
  <ul>
  <li><a href="https://developers.google.com/maps/articles/phpsqlsearch_v3" rel="nofollow noreferrer">Creating a Store Locator with PHP, MySQL &amp; Google Maps</a></li>
  </ul>
  
  <p><strong>EDIT</strong>: I have found a better one, this is very close, but still not what I am looking for, since it uses 1 bound of latitude and longitude coordinates when I have multiple in a table.</p>
  
  <ul>
  <li><a href="http://www.movable-type.co.uk/scripts/latlong-db.html" rel="nofollow noreferrer">Select points from a database by latitude/longitude within a bounding circle</a></li>
  </ul>