I am making a site in which I have to show all stores on a list sorted in a way that the nearer stores, to the users current location, come first and far ones come later. I need a MySQL query that selects all stores from database ORDER BY users current position. I have searched on Google a lot but could not find any query that matches my case.
You can try this:
SELECT temp1.* FROM (SELECT s.shop_id, s.shop_name, s.address, 3959 * ACOS( COS( RADIANS( 40.7127 ) ) * COS( RADIANS( s.latitude ) ) * COS( RADIANS( s.longitude ) - RADIANS( 74.0059 ) ) + SIN( RADIANS( 40.7127 ) ) * SIN( RADIANS( s.latitude ) ) ) AS distance FROM shop s) as temp1 WHERE temp1.distance <= 20 ORDER BY temp1.distance
it will give shop listing within 20 miles.
40.7127 is latitude and 74.0059 is longitude