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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.