Jump to content

Distance fomrula in a query.


cooldude832

Recommended Posts

I want to find all zip codes within X miles of a location so I am going to do the distance formula.

so I want something like this, but unsure on proper structure.

The max_distance is in longitude/latitude units as I know that 1 unit of them is 69 miles.

I'm just unsure if that If structure is proper, I want it to return the zip code of the results that are within the distance of it.

 

<?php
$zip_long = 42;
$zip_lat = 90;
$max_distance = "5";
$q = "Select IF(sqrt((lng-".$zip_lat.")^2+(lat-".$zip_long.")^2) <".$max_distance.", zip, 0) from `zip`";
?>

 

Link to comment
https://forums.phpfreaks.com/topic/85752-distance-fomrula-in-a-query/
Share on other sites

reworked it again, but now the results don't make sense

<sql>
SELECT (IF( (
sqrt( (
`lng` - 83.37159
)^2 + ( `lat` - 42.572442 ) ) ) <5/69, `zip` , NULL
)) as Zip
FROM `zips` 
</sql>

 

First off I want to know if there is a way to only get the matching results  Do I just remove the second part of my if the NULL part???

secondly its returning a jumbled set of Zips instead of what I anticipated to be numbers in the 483XX area

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.