SELECT ROUND(6371 * acos(cos(radians('lat')) * cos(radians(latitude)) * cos(radians(longitude) - radians('long')) + sin(radians('lat')) * sin(radians(latitude)))) as distance,latitude,longitude, from your_table HAVING distance<=20 order by distance
You can use a query similar to shown above in your SQL where 'lat'
and 'long'
are the variable values that you are passing to search for. The 'longitude'
and 'latitude'
are column names from your table. And btw, this is called Haversine formula and the constant 6371
is used to get distance in KM, while 3959
is used to get distance in miles. You can use either of them as per your requirement.