title: mySQL select zipcodes within x km/miles within range of y
body: <p><em>Note: Although I use a zipcode database with Dutch zipcodes, this question is country independent.</em></p>
  
  <p>I have a database with every zipcode in the Netherlands + its x and y coordinate (lat/long).</p>
  
  <p>I have for example zipcode: <code>$baseZipCode = 1044;</code> with the following coordinates:</p>
  
  <pre><code>x coordinate = 4,808855
  y coordinate = 52,406332
  </code></pre>
  
  <p>Now, I want to find all other zipcodes with <code>$range</code> from <code>$baseZipCode</code>. </p>
  
  <p>For example:</p>
  
  <pre><code>SELECT
    zipcode
  FROM
    zipcodes
  WHERE
    ????? // Need help here
  </code></pre>
  
  <p>The problem is that the earth is not completely round. I find a lot of tutorials with <code>from a to b</code> calculations but that's not what I need.</p>
  
  <p>Does anyone have any idea?</p>
  
  <hr>
  
  <p><strong>UPDATE</strong>
  Thanks to Captaintokyo I found this:</p>
  
  <p>Want to find all zipcodes and corresponding distances within a certain mile/kilometer radius from another zipcode or point? This problems require latitude and longitude coordinates to solve. Geocoding the address gives you latitude/longitude coordinates from an address. </p>
  
  <p>First you will need a database of all zipcodes and their corresponding latitude and longitude coordinates:</p>
  
  <pre><code>CREATE TABLE `zipcodes` (
    `zipcode` varchar(5) NOT NULL DEFAULT '',
    `city` varchar(100) NOT NULL DEFAULT '',
    `state` char(2) NOT NULL DEFAULT '',
    `latitude` varchar(20) NOT NULL DEFAULT '',
    `longitude` varchar(20) NOT NULL DEFAULT '',
    KEY `zipcode` (`zipcode`),
    KEY `state` (`state`)
  )
  </code></pre>
  
  <p>So once you have the database you want to find all zipcodes within a certain mile radius of a central point. If the central point is another zipcode, simply query the database for the latitude and longitude coordinates of that zipcode. Then the code is as follows:</p>
  
  <pre><code>// ITITIAL POINT
  
  $coords = array('latitude' =&gt; "32.8", 'longitude' =&gt; "-117.17");
  
  //RADIUS
  
  $radius = 30;
  
  // SQL FOR KILOMETERS
  
  $sql = "SELECT zipcode, ( 6371 * acos( cos( radians( {$coords['latitude']} ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( {$coords['longitude']} ) ) + sin( radians( {$coords['latitude']} ) ) * sin( radians( latitude ) ) ) ) AS distance FROM zipcodes HAVING distance &lt;= {$radius} ORDER BY distance";
  
  // SQL FOR MILES
  
  $sql = "SELECT zipcode, ( 3959 * acos( cos( radians( {$coords['latitude']} ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( {$coords['longitude']} ) ) + sin( radians( {$coords['latitude']} ) ) * sin( radians( latitude ) ) ) ) AS distance FROM zipcodes HAVING distance &lt;= {$radius} ORDER BY distance";
  
  // OUTPUT THE ZIPCODES AND DISTANCES
  
  $query = mysql_query($sql);
  
  while($row = mysql_fetch_assoc($query)){
  
      echo "{$row['zipcode']} ({$row['distance']})&lt;br&gt;\n";
  
  }
  </code></pre>
  
  <p>(Both Yahoo and Google offer free geocoding services.)</p>