-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
  • 156
  • 1
  • 15

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
  • 22,212
  • 11
  • 70
  • 132
Muhammad Muazzam
  • 2,810
  • 6
  • 33
  • 62