1SELECT id,
2( 6371 *
3 ACOS(
4 COS( RADIANS( db_latitude ) ) *
5 COS( RADIANS( $user_latitude ) ) *
6 COS( RADIANS( $user_longitude ) -
7 RADIANS( db_longitude ) ) +
8 SIN( RADIANS( db_latitude ) ) *
9 SIN( RADIANS( $user_latitude) )
10 )
11)
12AS distance FROM the_table HAVING distance <= $the_radius ORDER BY distance ASC"
13