Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FUNCTION ST_Distance_Sphere does not exist in MariaDB

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)
like image 673
webafra Avatar asked Jun 07 '17 09:06

webafra


3 Answers

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
like image 113
Carlos Poma Avatar answered Oct 09 '22 02:10

Carlos Poma


10.2+

This issue has been fixed and backported with MDEV-13467. It's available 10.2.38, 10.3.29, 10.4.19, 10.5.10

Find their support matrix here.

like image 39
NO WAR WITH RUSSIA Avatar answered Oct 09 '22 03:10

NO WAR WITH RUSSIA


http://mysql.rjweb.org/doc.php/find_nearest_in_mysql#gcdistdeg

That blog discusses multiple ways of "finding nearest" on the globe in MySQL/MariaDB. As part of that discussion, I developed that Stored Function.

like image 30
Rick James Avatar answered Oct 09 '22 03:10

Rick James