【问题标题】:Return matching sql rows based on distance根据距离返回匹配的 sql 行
【发布时间】:2016-01-13 16:51:39
【问题描述】:

其实很简单,就是不知道怎么做。

我有一张桌子:

table locations 
location_id     coordinates    name
-------------------------------------

当我从设备接收到纬度和经度时,我将其存储在 mysql 中 POINT(Long, Lat) 类型的变量中。

我正在尝试获取 Locations 表中距离小于或等于 8 米的所有行。

我的查询如下:

SELECT * FROM locations where st_distance_sphere(@userCoords, @pt1)  <= 8; 

我知道这个查询是错误的,因为@pt1 与列坐标无关。如何隔离坐标列中的特定值并返回匹配的行列表?

st_distance_sphere() 函数只计算两个坐标之间的距离并返回一个数字。

提前感谢您!

【问题讨论】:

    标签: mysql sql database gis


    【解决方案1】:

    您只需要放置列而不是@pt1(假设列类型为Point):

    SELECT * 
    FROM locations 
    where st_distance_sphere(@userCoords, coordinates)  <= 8; 
    

    这将为您提供坐标为@userCoords 8 或更少的所有行

    【讨论】:

    • 救世主。感谢您帮助新手:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    相关资源
    最近更新 更多