Tag Archives: find location with latitude longitude in google maps
Get zip codes in radius Mysql query
Get zip codes in radius Mysql query
You can get nearest zip codes in the area simply using the following mysql query instead of processing in programming language.
Lets suppose we have a table which contains the zip groups alng with the latitude and longitude.
CREATE TABLE `zip_codes` ( `zip_code` varchar(5) NOT NULL DEFAULT '', `city` varchar(100) NOT NULL DEFAULT '', `state` char(2) NOT NULL DEFAULT '', `latitude` varchar(20) NOT NULL DEFAULT '', `longitude` varchar(20) NOT NULL DEFAULT '', KEY `zip_code` (`zip_code`) )
This Table Contains the zip group list.
Query to find the zip codes within the given radius
Get zip codes in radius Mysql query in Kilometers
SELECT zip_code, ( 6371 * acos( cos( radians( $latitude ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( $longitude ) ) + sin( radians( $latitude) ) * sin( radians( latitude ) ) ) ) AS distance FROM zipcodes HAVING distance <= {$radius} ORDER BY distance;
Get zip codes in radius Mysql query in Miles
SELECT zip_code, ( 3959 * acos( cos( radians( $latitude ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( $longitude ) ) + sin( radians( $latitude) ) * sin( radians( latitude ) ) ) ) AS distance FROM zipcodes HAVING distance <= $radius ORDER BY distance;