Mr x
Mr x

Reputation: 868

Fetch records with 10km of distance in MYSQL

I have table in MYSQL along with lat & lon values.

I want to fetch the users in the radius of 10km in context of current lat & lon.

I am trying following query but it gives me empty output.

SELECT
`id`,
`name`,
ACOS( SIN( RADIANS( `latitude` ) ) * SIN( RADIANS( $fLat ) ) + COS( RADIANS( `latitude` ) )
* COS( RADIANS( $fLat )) * COS( RADIANS( `longitude` ) - RADIANS( $fLon )) ) * 6380 AS `distance`
FROM `users`
WHERE
ACOS( SIN( RADIANS( `latitude` ) ) * SIN( RADIANS( $fLat ) ) + COS( RADIANS( `latitude` ) )
* COS( RADIANS( $fLat )) * COS( RADIANS( `longitude` ) - RADIANS( $fLon )) ) * 6380 < 10
ORDER BY `distance`

where latitude and longitude are columns names & $fLat & $fLon are current lat and lon values.

Upvotes: 0

Views: 778

Answers (1)

Ahtesham Shah
Ahtesham Shah

Reputation: 65

Use this query your problem will be solved:

SELECT `id`, `name`, `phone`, `latitude`, `longitude`, SQRT( POW(69.1 * (`latitude` - 24.900363), 2) + POW(69.1 * (67.099760 - `longitude`) * COS(`latitude` / 57.3), 2))
AS `distance`
FROM `users` HAVING `distance` < 25
ORDER BY `distance`

Upvotes: 2

Related Questions