FUNCTION ST_Distance_Sphere does not exist in MariaDB

青春壹個敷衍的年華 提交于 2019-12-07 06:42:57

问题


I want get all locations around my location but the function ST_Distance_Sphere does not work.

My query:

select *, astext(location) as location from `locations`
where ST_Distance_Sphere(location, POINT(35.905069591297, 49.765869174153)) < 1000

Error :

SQLSTATE[42000]: Syntax error or access violation:
1305 FUNCTION app.ST_Distance_Sphere does not exist (SQL:
select *, astext(location) as location from `locations`
where ST_Distance_Sphere(location, POINT(35.905069591297, 49.765869174153)) < 1000)

回答1:


A direct copy of my answer on DBA.SE,


Almost unbelievably, MariaDB lacks this ST_Distance_Sphere (MDEV-13467).

Find their support matrix here.

And it's not just that either, it also lacks ST_GeoHash that MySQL has.




回答2:


For those who still need the function in MariaDB, you can create the function based on the formula

    CREATE FUNCTION `st_distance_sphere`(`pt1` POINT, `pt2` POINT) RETURNS 
    decimal(10,2)
    BEGIN
    return 6371000 * 2 * ASIN(SQRT(
       POWER(SIN((ST_Y(pt2) - ST_Y(pt1)) * pi()/180 / 2),
       2) + COS(ST_Y(pt1) * pi()/180 ) * COS(ST_Y(pt2) *
       pi()/180) * POWER(SIN((ST_X(pt2) - ST_X(pt1)) *
       pi()/180 / 2), 2) ));
    END



回答3:


I think that's it ST_DISTANCE https://mariadb.com/kb/en/library/st_distance/



来源:https://stackoverflow.com/questions/44409012/function-st-distance-sphere-does-not-exist-in-mariadb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!