Search for a certain distance : Distance « Geometric « SQL / MySQL






Search for a certain distance

    
mysql>
mysql> CREATE TABLE cities (
    ->   id int(11) ,
    ->   pt point NOT NULL default '',
    ->   zip int(11) default NULL,
    ->   country varchar(10) default NULL,
    ->   state char(2) default NULL,
    ->   city varchar(100) default NULL,
    ->   district varchar(100) default NULL
    -> );
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql>
mysql>
mysql>
mysql> SELECT ROUND(SQRT(POW(@x0 - X(pt), 2) + POW(@y0 - Y(pt), 2)) / 1000) AS distance,
    -> city FROM cities
    -> HAVING distance<=30 ORDER BY distance;
Empty set (0.00 sec)

mysql>
mysql> drop table cities;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
  








Related examples in the same category

1.Calculates the actual distances and excludes the cities that are too far away.