mysql选择100公里内的数据

时间:2017-04-21 09:29:06

标签: mysql

MySQL查询:

select pet_info.pet_user_id as userid,
pet_info.pet_cat,pet_info.pet_breed as petbreed,
lostpets.pet_reward,lostpets.currency,pet_info.pet_name as name,
lostpets.pet_lost_date as date,
lostpets.pet_city,lostpets.petid as pid,
lostpets.id as lid,lostpets.type,lostpets.pet_lost_location,lostpets.pet_lost_address,lostpets.pet_postal,lostpets.pet_country 
,( 6371 * ACOS( COS( RADIANS( '23.0862143' ) ) * COS( RADIANS(  `pet_lat` ) ) * COS( RADIANS(  `pet_long` ) - RADIANS( '72.59330969999996' ) ) + SIN( RADIANS( '23.0862143' ) ) * SIN( RADIANS(  `pet_lat` ) ) ) ) AS `distance`
from lostpets as lostpets 
LEFT JOIN pet_info as pet_info ON lostpets.petid=pet_info.id 
where lostpets.active='Active' AND `distance` < 100  order by `distance` asc

此查询显示两个lat和long之间的距离。但我想选择100公里范围内的数据。我怎么能这样帮助我。

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT 
    pet_info.pet_user_id as userid,
    pet_info.pet_cat,
    pet_info.pet_breed as petbreed,
    lostpets.pet_reward,
    lostpets.currency,
    pet_info.pet_name as name,
    lostpets.pet_lost_date as date,
    lostpets.pet_city,
    lostpets.petid as pid,
    lostpets.id as lid,
    lostpets.type,
    lostpets.pet_lost_location,
    lostpets.pet_lost_address,
    lostpets.pet_postal,
    lostpets.pet_country,
    ( 6371 * ACOS( COS( RADIANS( '23.0862143' ) ) * COS( RADIANS(  `pet_lat` ) ) * COS( RADIANS(  `pet_long` ) - RADIANS( '72.59330969999996' ) ) + SIN( RADIANS( '23.0862143' ) ) * SIN( RADIANS(  `pet_lat` ) ) ) ) AS `distance`
FROM lostpets as lostpets 
LEFT JOIN pet_info as pet_info 
ON lostpets.petid = pet_info.id 
HAVING `distance` < 100
WHERE lostpets.active = 'Active' 
ORDER BY `distance` asc