Skip to content

Instantly share code, notes, and snippets.

@hopeseekr
Created March 21, 2018 17:59
Show Gist options
  • Save hopeseekr/af38375aaeee63e80253b9a119359467 to your computer and use it in GitHub Desktop.
Save hopeseekr/af38375aaeee63e80253b9a119359467 to your computer and use it in GitHub Desktop.
lab optimization
SET @dist = 20;
SET @latDist = @dist / 69.172;
SELECT zip_codes.*
FROM labs
JOIN zip_codes ON zip_codes.zip_code=labs.zip_code COLLATE utf8_unicode_ci
WHERE zip_codes.zip_code BETWEEN 70000 AND 79999
AND labs.latitude < zip_codes.latitude + @latDist
AND labs.latitude > zip_codes.latitude - @latDist
AND SQRT(
POW(69.1 * (labs.latitude - zip_codes.latitude), 2) +
POW(69.1 * (zip_codes.longitude - labs.longitude) * COS(labs.latitude / 57.3), 2)
) <= @dist;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment