minggehenhao 2019-06-30
CREATE TABLE `map` ( `id` int(11) NOT NULL, `address` varchar(255) NOT NULL DEFAULT '', `location` geometry NOT NULL, PRIMARY KEY (`id`), SPATIAL KEY `idx_location` (`location`) )
INSERT INTO map (id, address, location) VALUES (1, 'somewhere', ST_GeomFromText('POINT(121.366961 31.190049)'));注意必须使用
ST_GeomFromText
函数,且 POINT()
里面是:经度+空格+纬度SELECT address, ST_AsText(location) AS location FROM map;
SELECT ST_Distance_Sphere(POINT(121.590347, 31.388094),location) AS distant FROM map;算出来的结果,单位是米
SELECT id, address, ST_Distance_Sphere(POINT(121.590347, 31.388094),location) AS distant FROM map WHERE ST_Distance_Sphere(POINT(121.590347, 31.388094),location) < 1000 ORDER BY distant;