我有一个按照以下示例结构的mysql表:
POSTAL_CODE_ID|PostalCode|City|Province|ProvinceCode|CityType|Latitude|Longitude 7|A0N 2J0|Ramea|Newfoundland|NL|D|48.625599999999999|-58.9758 8|A0N 2K0|Francois|Newfoundland|NL|D|48.625599999999999|-58.9758 9|A0N 2L0|Grey River|Newfoundland|NL|D|48.625599999999999|-58.9758
现在我想做的是创建一个查询,该查询将在搜索位置的选定公里内选择结果
因此,假设他们搜索“灰色河流”,然后选择“在20公里以内找到所有结果”
它显然应该选择“灰河”,但也应根据经度和纬度选择灰河20公里以内的所有位置。
我真的不知道该怎么做。我已经读过haversine公式,但不知道如何将其应用于mysql SELECT。
任何帮助将非常感激。
SELECT * FROM mytable m JOIN mytable mn ON ACOS(COS(RADIANS(m.latitude)) * COS(RADIANS(mn.latitude)) * COS(RADIANS(mn.longitude) - RADIANS(m.longitude)) + SIN(RADIANS(m.latitude)) * SIN(radians(mn.latitude))) <= 20 / 6371.0 WHERE m.name = 'grey river'
如果是表,则MyISAM可能要以本机几何格式存储点并SPATIAL在其上创建索引:
MyISAM
SPATIAL
ALTER TABLE mytable ADD position POINT; UPDATE mytable SET position = POINT(latitude, longitude); ALTER TABLE mytable MODIFY position NOT NULL; CREATE SPATIAL INDEX sx_mytable_position ON mytable (position); SELECT * FROM mytable m JOIN mytable mn ON MBRContains ( LineString ( Point ( X(m.position) - 0.009 * 20, Y(m.position) - 0.009 * 20 / COS(RADIANS(X(m.position))) ), Point ( X(m.position) + 0.009 * 20, Y(m.position) + 0.009 * 20 / COS(RADIANS(X(m.position)) ) ), mn.position ) AND ACOS(COS(RADIANS(m.latitude)) * COS(RADIANS(mn.latitude)) * COS(RADIANS(mn.longitude) - RADIANS(m.longitude)) + SIN(RADIANS(m.latitude)) * SIN(radians(mn.latitude))) <= 20 / 6371.0 WHERE m.name = 'grey river'