MySQL查询返回错误

时间:2011-06-22 15:57:40

标签: mysql sql

我正在尝试从数据库中获取坐标和位置,但我的服务器正在返回此错误:

Error: 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 
'long ) - radians(0) ) + sin( radians(0) ) * sin( radians( lat ) ) ) ) AS distanc' 
 at line 1

Query: 
SELECT id, 
    address, 
    name, 
    (3959 * acos(cos(radians(0)) * 
           cos(radians(lat)) * 
           cos(radians(long) - radians(0)) + 
           sin(radians(0)) * 
           sin(radians(lat)))
    ) AS distance 
FROM places 
HAVING distance < 10 
ORDER BY distance 
LIMIT 0, 20;

我是否必须使用`来逃避“lat”和“long”,或者该公式是否完全错误?感谢。

另外,我使用的坐标是0,0(仅用于测试目的)。

3 个答案:

答案 0 :(得分:2)

LONG是mysql中的保留字。将它用作列名称时使用反引号将其转义。

答案 1 :(得分:1)

用“WHERE”语句替换HAVING语句。没有“GROUP BY”,那你为什么要使用HAVING?

答案 2 :(得分:0)

我认为你有一个额外的)。试试这个

SELECT id, address, name, ( 3959 * acos( cos( radians(0) ) * cos( radians( lat ) ) * cos( radians( long ) - radians(0) ) + sin( radians(0) ) * sin( radians( lat ) ) ) AS distance FROM places HAVING distance < 10 ORDER BY distance LIMIT 0 , 20;