我在mysql调用BRANCH中有一个表
===============================
Branch_id latitude longitude
===============================
1 3.109421 101.622913
2 3.101121 101.644913
当我传递当前位置的纬度/经度时,如何从该表中选择公里计算?
示例:
如果我通过当前位置= 3.122221 101.343913
============================================
Branch_id latitude longitude distance(km)
============================================
1 3.109421 101.622913 0.4
2 3.101121 101.644913 0.6
已编辑(已解决)
SELECT p.title,p.subtitle,p.desc,p.image,p.promotion_id,p.merchant_id,p.date_from,p.date_to,m.merchant_name,p.view,mb.latitude,mb.longitude,
(6371 * acos (cos( radians(3.158704)) * cos( radians(mb.latitude))
* cos( radians(mb.longitude) - radians(101.713963)) + sin(radians(3.158704))
* sin( radians(mb.latitude)))) AS distance
FROM merchant_branch as mb
left join merchant m on mb.merchant_id = m.merchant_id
left join promotion p on p.merchant_id = m.merchant_id
where p.promotion_id is not null order by distance asc
答案 0 :(得分:1)
发件人:https://developers.google.com/maps/solutions/store-locator/clothing-store-locator
这是在一个SQL语句中查找最近的20个位置的SQL语句 -33、151座标的半径为25英里。它计算 基于该行的纬度/经度和目标的距离 纬度/经度,然后仅询问距离 值小于25,按距离和限制对整个查询进行排序 它到20结果。要按公里而不是英里进行搜索,请替换 3959和6371。
SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;