mysql计算附近的地铁站
大家讲道理
大家讲道理 2017-04-17 15:47:24
0
2
573

如图有两张表,小区表(xiaoqu)和地铁表(metro),现在小区表里面小区附近的地铁站(图片中红色字段)是空的。想批量完善这个字段,求助
附近的定义是:离小区最近,并且在1000米以内的一个地铁站。
例如:北京的华腾园小区,附近1000米有双井和劲松两个站,但是劲松距离较近,所以华腾园那一行对应的就是劲松站。

目前已经自定义了两个经纬度之间距离的函数。getdistance(纬度1,经度1,纬度2,经度2),函数代码如下:

-- getdistance(Lat1 ,Lon1,Lat2 ,Lon2)
BEGIN 
    DECLARE
        dis DOUBLE ;
    SET dis = ACOS(
        SIN((Lat1 * 3.1415) / 180) * SIN((Lat2 * 3.1415) / 180) + COS((Lat1 * 3.1415) / 180) * COS((Lat2 * 3.1415) / 180) * COS(
            (Lon1 * 3.1415) / 180 - (Lon2 * 3.1415) / 180
        )
    ) * 6370996.81;
RETURN dis ;
END
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
PHPzhong

My idea is: get the target point coordinates -> Use a certain range r to check which subways in the subway table are in r. If there are, compare the distances. If r is not increased, repeat until the r distance exceeds the maximum range -&gt ;Finish.

I think the key to efficiency lies in calculating and comparing distances.

大家讲道理

I think this should not be implemented using SQL. SQL去实现。

我去做的话:

  1. 获取小区信息,获取属于哪座城市

  2. SQL当前城市地铁数据搜出来。

  3. 然后可以用各种算法

    If I do it:
    Get community information and which city it belongs to.

  4. Use SQL to search for current city subway data.

    🎜
  5. 🎜Then you can use various algorithms (or rules, such as your method above) to calculate the nearest subway. 🎜🎜 🎜 🎜🎜SQL should be used to store and obtain data, and should not carry too many logical functions🎜🎜
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template