16

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)
Rick James
  • 135,179
  • 13
  • 127
  • 222
webafra
  • 221
  • 1
  • 2
  • 8
  • My DataBase is MariaDb – webafra Jun 08 '17 at 05:36
  • The formula with lots of trig calls works MySQL or MariaDB. MySQL only recently (5.7.6) implemented that ST function; MariaDB does not seem to have picked it up yet. – Rick James Jun 08 '17 at 05:43
  • i see this link but ST_Distance_Sphere function is not Similar ! [MariaDb Docs](https://mariadb.com/kb/en/mariadb/mysqlmariadb-spatial-support-matrix/) – webafra Jun 08 '17 at 05:49
  • The formulas need latitudes and longitudes. But that is what your `POINT` is composed of, correct? I am suggesting a somewhat messy workaround, not a simple drop-in replacement. – Rick James Jun 08 '17 at 19:13
  • I angry From MariaDB :| i change DataBase to PostgreSQL ! :| – webafra Jun 09 '17 at 06:12

4 Answers4

15

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
Carlos Poma
  • 199
  • 2
  • 7
  • I had to delete "BEGIN". Also, I guess a spatial index does not work with this function. Correct? – moritz.vieli Aug 13 '20 at 06:14
  • @moritz.vieli .True, BEGIN is not required for single line routine. As long as indexes are used on base tables, the functions can be used in Views, Stored Procedures, or Triggers. Or what do you mean? – Carlos Poma Aug 14 '20 at 05:06
  • Let's assume, I have a SELECT statement where I sort by st_distance_sphere. Does MariaDB use the spatial index I added on a column, which is used as a parameter for the function? – moritz.vieli Aug 14 '20 at 08:53
  • well, obviously not with this self-implemented function, rendering the spatial index in mariadb completely useless. – user3700562 Jul 25 '21 at 11:12
9

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.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
1

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.

Rick James
  • 135,179
  • 13
  • 127
  • 222
-3

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