Posted by J.O. Aho on 11/03/06 06:02
Tyrone Slothrop wrote:
> I need to write a query which will find those records which have the
> closest values to a zip code posted through a form. The values may be
> greater than or less than the requested value.
>
> I had written a function which accomplishes this which uses multiple
> queries many years ago. As I am now revising the site in a major
> redesign, the thought occurred to me that there must be a better way.
>
> Any ideas?
If you have number only zip codes (not all countries has that), then you can
use a simple interval limitation (as numbers close to another usually are
close in geographical terms too, but not always true).
$query="SELECT * FROM ZipTable WHERE (ZipCode > $ZipNumber - $Range) AND
(ZipCode < $ZipNumber + $Range)";
Where we use two variables set by the php-script, $ZipNumber is the zip code
we are searching for and $Range is the value how much larger or smaller the
zip code is for those which are "near".
It's of course a question how you determine what is close, so this simple
query may not be what you was looking for.
//Aho
[Back to original message]
|