如何从表中获得10个最近的位置

时间:2013-05-22 09:38:01

标签: mysql latitude-longitude closest-points mysql-select-db

我希望获得经度和纬度最近的10个地点。我将经度和纬度存储为双倍。我试过这个:

SELECT * FROM company as dest where 3956 * 2 * ASIN(SQRT( POWER(SIN((122.4058 - abs( dest.latitude)) * pi()/180 / 2),2) + COS(122.4058 * pi()/180 ) * COS( abs (dest.latitude) * pi()/180) * POWER(SIN((37.7907 – dest.longitude) * pi()/180 / 2), 2) )) as dis <10 and ORDER BY dis limit 10; 

但是我收到了这个错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '– dest.longitude) * pi()/180 / 2), 2) )) as dis <10 and ORDER BY dis limit 10 ' at line 1

如何解决此错误?

2 个答案:

答案 0 :(得分:1)

我复制了你的sql-code并将其放入一个具有语法高亮支持(Sublime2)的编辑器和字符,错误指向(看起来像一个减号)实际上是另一个标志..

根据这个网页,这个字符被称为EN DASH(你可以在链接末尾的utf8-hex-code中看到它:)) http://www.utf8-character.info/#!%E2%80%93

尝试用简单的减号替换此字符。

答案 1 :(得分:1)

试试这个:

     POWER(SIN((37.7907 – abs(dest.longitude))

EDIT2:

     SELECT *,3956 * 2 * ASIN(SQRT( POWER(SIN((122.4058 - abs(dest.latitude)) * pi()/180 / 2),2) + COS(122.4058 * pi()/180 ) * COS( abs(dest.latitude) * pi()/180) * POWER(SIN((37.7907 - abs(dest.longitude)) * pi()/180 / 2), 2) )) as dis FROM company as dest HAVING dis <10 ORDER BY dis limit 10;