Join operation in Haversine formula

后端 未结 2 1757
悲&欢浪女
悲&欢浪女 2021-02-10 18:29

I am implementing Haversine formula in PHP as the follows

$result=mysqli_query($mysqli,\"SELECT *,( 6371 * acos( cos( radians({$lat}) ) * cos( radians( `latitude         


        
2条回答
  •  悲哀的现实
    2021-02-10 18:54

    In this case, put the first query as a derived subquery in the second:

    SELECT  p.*, f.*    -- Select only the columns you need, not all
        FROM  
        (
            SELECT  *,
                    ( 6371 * acos( cos( radians({$lat}) ) * cos( radians( `latitude` ) )
                      * cos( radians( `longitude` ) -radians({$lon}) )
                      +sin( radians({$lat}) ) * sin( radians( `latitude` ) ) )
                    ) AS distance
                FROM  `places`
                HAVING  distance <= {$radius}
                ORDER BY  distance ASC"
                LIMIT 10               -- Didn't you forget this??
        ) AS p
        JOIN  my_friends AS f  ON f.personal_id p.personal_id
          AND  id='".$rest_time."'"     -- Huh??
    

提交回复
热议问题