php mysql compare long and lat, return ones under 10 miles

前端 未结 6 1679
借酒劲吻你
借酒劲吻你 2020-12-15 00:35

Hay i want to find the distance (in miles) between 2 locations using lat and long values, and check if they are within a 10 mile radius of each other.

When a user lo

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 01:24

    Before your query, calculate the maximum and minimum latitude and longitude of your ten-mile circle. This will give you four numbers so the first part of your where clause will exclude any rows which fall outside of an approximate 10 mile-each way box.

    Then the complicated real distance where clause checks only the ones inside the square, to see if they're also inside the circle.

    Read your DBMS's docs to see whether lazy evaluation will apply or if you will need to arrange it as

    SELECT *
    FROM (SELECT *
          FROM table
          WHERE (simple rough box clause)
         )
    WHERE (complex clause)
    

    instead of the usual

    SELECT * FROM table WHERE (simple clause) AND (complex clause)
    

提交回复
热议问题