Call Us: 992 9441 754

Calculate Distance Using Latitude and Longitude in MySQL

< All Topics

Calculate Distance Using Latitude and Longitude in MySQL

To calculate the distance between two points specified by their latitude and longitude coordinates in MySQL, you can use the Haversine formula. The Haversine formula calculates the shortest distance between two points on the surface of a sphere given their latitudes and longitudes.

Here’s how you can create a MySQL function to calculate the distance using the Haversine formula:

This function takes four parameters: the latitude and longitude of the first point (lat1, lon1), and the latitude and longitude of the second point (lat2, lon2). It calculates the distance between these two points using the Haversine formula and returns the distance in kilometers.

You can then use this function in your SQL queries to calculate the distance between points. For example:

This query will calculate the distance between New York City (latitude 40.7128, longitude -74.0060) and Los Angeles (latitude 34.0522, longitude -118.2437) in kilometers.

Categories

Recent Comments

No comments to show.