Romil Singh Tomar
Romil Singh Tomar

Reputation: 25

I want to fetch rows from mysql database with filter by near by and the limit of distance

I want to fetch rows from MySql database with filter by near by and the limit of distance. I have created a query that is working fine for fetching the data near by, but I want to add a condition for distance limitation, that means If I want rows which have location of 20 km distance. This is the query that is working for near by.

$lat="";
$lon="";
$sql="SELECT *, 
  111.111 *
    DEGREES(ACOS(COS(RADIANS(".$lat."))
         * COS(RADIANS(Lat))
         * COS(RADIANS(".$lon." - Lon))
         + SIN(RADIANS(".$lat."))
         * SIN(RADIANS(Lat)))) AS d
FROM Table
ORDER BY d"

Upvotes: 0

Views: 66

Answers (1)

Amit Sahu
Amit Sahu

Reputation: 996

You can use HAVING Clause to achieve this

$sql="SELECT *, 
  111.111 *
    DEGREES(ACOS(COS(RADIANS(".$lat."))
         * COS(RADIANS(Lat))
         * COS(RADIANS(".$lon." - Lon))
         + SIN(RADIANS(".$lat."))
         * SIN(RADIANS(Lat)))) AS d
FROM Table HAVING d <= 20
ORDER BY d"

Upvotes: 1

Related Questions