-1

Can anyone help me with that. at mysql database I have providers table which contains longitude and latitude attributes, there is a user who will looking for providers who will be after x km from him so suppose that I have the user longitude and latitude. Is there any way to do that with mysql

Mohamed Salah
  • 147
  • 1
  • 14

1 Answers1

4

It requires MySQL’s built-in Math functions, including cos(), sin(), acos() and radians().

SELECT id, ( 3959 * acos( cos( radians(latitude) ) *
              cos( radians( latitude) ) 
        * cos( radians( longitude ) - radians(longitude ) )
        + sin( radians(latitude) ) * sin(radians(latitude)) ) 
    ) AS distance 
    FROM myTable

Where value 3959 is the Earth radius in miles

Martin
  • 20,858
  • 7
  • 60
  • 113
Muhammad Muazzam
  • 2,752
  • 6
  • 30
  • 57