2010-02-16 19 views
7

Supongamos que tengo una tabla de 2 columnas como esta:Encuentra la distancia entre dos puntos en MYSQL. (Utilizando el tipo de datos Point)

| user_id  | int(11) | NO | UNI | NULL |    | 
| utm   | point | NO | MUL | NULL |    | 

Como se puede ver, es muy simple. utm es un Punto tipo de datos. Lo inserto de esta manera:

INSERT INTO mytable(user_id, utm) VALUES(1, PointFromWKB(point(50, 50))); 

Luego, creo un índice espacial.

ALTER TABLE mytable ...add spatial index on(utm) or something. (forgot) 

Muy bien, todo está bien. Ahora, quiero seleccionar * donde distancia < 99999. Pero no funciona!

//This is supposed to select all where the distance is less than 99999999. 
set @mypoint = PointFromWKB(point(20,20)) 
select * from mytable where GLength(LineString(utm, @mypoint)) < 9999999; 
Empty set (0.00 sec) 
select * from mytable where GLength(LineStringFromWKB(LineString(utm, @mypoint))) < 9999; 
Empty set (0.00 sec) 

Por cierto, me han tratado de INSERT INTO sin la PointFromWKB ... y no funcionó ... por eso alguien sugirió que PointFromWKB a mí.

Respuesta

3

Resuelto. Esto es lo que hice:

where GLength(LineStringFromWKB(LineString(asbinary(utm), asbinary(@mypoint)))) < 9999999999999; 
+0

Calcula la distancia según coordenadas geográficas o coordenadas planas. –

1

También puede hacerlo de esta manera. No estoy seguro de si es más rápido o no.

select * from mytable where glength(geomfromtext(concat('linestring(', x(utm), ' ', y(utm), ',20 20', ')'))) < 99999999 
0

Por lo que yo sé, u tiene que probar esta manera-

select * from mytable 
    where 
    (
     GLength(
      LineStringFromWKB(
      LineString(
       geoPoint, 
       GeomFromText('POINT(51.5177 -0.0968)') 
      ) 
     ) 
     ) 
    ) < 99999999 

Más en this answer.

Cuestiones relacionadas