PHP/MySQL: Select locations close to a given location from DB

后端 未结 7 1537
隐瞒了意图╮
隐瞒了意图╮ 2021-02-02 03:15

In PHP, I have the following code for calculating the distance between two locations:



        
7条回答
  •  被撕碎了的回忆
    2021-02-02 03:50

    Maybe something like

    SELECT field1, field2, ...,
        ACOS(SIN(latitude / 180 * PI()) * SIN(:1) + COS(latitude / 180 * PI()) * COS(:2) * COS(:2 - longtidude)) * 6371 AS distance
        ORDER BY distance ASC;
    

    or

    SELECT field1, field2, ...,
        ACOS(SIN(RADIANS(latitude)) * SIN(:1) + COS(RADIANS(latitude)) * COS(:2) * COS(:2 - longtidude)) * 6371 AS distance
        ORDER BY distance ASC;
    

    (directly translated from the PHP code)

    :1 and :2 is $lat2/180*pi() and $long2/180*pi() respectively.

提交回复
热议问题