title: MySQL Great Circle Distance (Haversine formula)
body: <p>I've got a working PHP script that gets Longitude and Latitude values and then inputs them into a MySQL query. I'd like to make it solely MySQL. Here's my current PHP Code:</p>
  
  <pre><code>if ($distance != "Any" &amp;&amp; $customer_zip != "") { //get the great circle distance
  
      //get the origin zip code info
      $zip_sql = "SELECT * FROM zip_code WHERE zip_code = '$customer_zip'";
      $result = mysql_query($zip_sql);
      $row = mysql_fetch_array($result);
      $origin_lat = $row['lat'];
      $origin_lon = $row['lon'];
  
      //get the range
      $lat_range = $distance/69.172;
      $lon_range = abs($distance/(cos($details[0]) * 69.172));
      $min_lat = number_format($origin_lat - $lat_range, "4", ".", "");
      $max_lat = number_format($origin_lat + $lat_range, "4", ".", "");
      $min_lon = number_format($origin_lon - $lon_range, "4", ".", "");
      $max_lon = number_format($origin_lon + $lon_range, "4", ".", "");
      $sql .= "lat BETWEEN '$min_lat' AND '$max_lat' AND lon BETWEEN '$min_lon' AND '$max_lon' AND ";
      }
  </code></pre>
  
  <p>Does anyone know how to make this entirely MySQL? I've browsed the Internet a bit but most of the literature on it is pretty confusing. </p>