2010-02-16 35 views
7

Supponiamo che ho una tabella 2 colonna in questo modo:Trova la distanza tra due punti in MYSQL. (Utilizzando il Punto Tipo di dati)

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

Come si può vedere, è molto semplice. utm è un tipo di dati Punto. Lo inserisco così:

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

Quindi, creo un indice spaziale.

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

OK, va tutto bene. Ora, voglio selezionare * dove distanza < 99999. Ma non funziona!

//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) 

A proposito, ho cercato di INSERT INTO senza il PointFromWKB ... e non ha funzionato ... è per questo che qualcuno ha suggerito che PointFromWKB a me.

risposta

3

Risolto. Questo è quello che ho fatto:

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

Calcola la distanza in base a coordinate geografiche o coordinate planari. –

1

Si può anche fare in questo modo. Non sono sicuro se sia più veloce o meno.

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

Per quanto ne so, u deve provare questo modo-

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

Altro a this answer.

Problemi correlati