Home >Database >Mysql Tutorial >## How to Improve the Accuracy of Radius-Based Location Searches in MySQL?
Improving Accuracy in MySQL Longitude and Latitude Query for Radius-Based Search
When performing radius-based location searches in mySQL, a common issue is obtaining results that deviate significantly from the specified radius. This discrepancy often arises from errors in the distance calculation formula, leading to inaccurate distance measurements.
To remedy this problem, consider a refined query that utilizes a slightly different distance calculation method:
SELECT `id`, ( 6371 * acos( cos( radians( :lat ) ) * cos( radians( `lat` ) ) * cos( radians( `long` ) - radians( :long ) ) + sin(radians(:lat)) * sin(radians(`lat`)) ) ) `distance` FROM `location` HAVING `distance` < :distance ORDER BY `distance` LIMIT 25
In this query:
This formula calculates the distance based on the Haversine formula, which provides more accurate results than the formula used in your original query. By incorporating this revised formula, you can refine your radius search functionality, ensuring that the returned results adhere to the specified radius range.
The above is the detailed content of ## How to Improve the Accuracy of Radius-Based Location Searches in MySQL?. For more information, please follow other related articles on the PHP Chinese website!