在mysql中传入2值(langtitude和longitude)时如何获得以公里为单位的距离?

f5emj3cl  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(453)

我在mysql call 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
b5lpy0ml

b5lpy0ml1#

发件人:https://developers.google.com/maps/solutions/store-locator/clothing-store-locator
下面是sql语句,它在距离-33151坐标25英里的半径范围内查找最近的20个位置。它基于该行的纬度/经度和目标纬度/经度计算距离,然后仅请求距离值小于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;

相关问题