cooldude832 Posted January 13, 2008 Share Posted January 13, 2008 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`"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/85752-distance-fomrula-in-a-query/ Share on other sites More sharing options...
cooldude832 Posted January 13, 2008 Author Share Posted January 13, 2008 reworked my query to this Select IF((sqrt((`lng`-83.37159)^2+(`lat`-42.572442)^2)) <5, zip) from `zips` "Using static values, but I think the math isn't right. Quote Link to comment https://forums.phpfreaks.com/topic/85752-distance-fomrula-in-a-query/#findComment-437653 Share on other sites More sharing options...
cooldude832 Posted January 13, 2008 Author Share Posted January 13, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/85752-distance-fomrula-in-a-query/#findComment-437658 Share on other sites More sharing options...
Barand Posted January 13, 2008 Share Posted January 13, 2008 this thread may help http://www.phpfreaks.com/forums/index.php/topic,175951.0.html or this http://www.phpfreaks.com/forums/index.php/topic,166487.msg733106.html#msg733106 Quote Link to comment https://forums.phpfreaks.com/topic/85752-distance-fomrula-in-a-query/#findComment-438188 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.