复杂查询的MySQL语法错误

时间:2013-05-16 20:57:06

标签: mysql sql database syntax

很抱歉这个derp问题(我确定它可能是这样),但是经过手册并通过Stack Overflow回答这个问题后,我仍然不确定这个数据库查询有什么问题。

一些信息: 我正在尝试创建一个函数,该函数在另一个zipcode的半径范围内创建所有zipcodes的数组。我使用它作为参考:http://www.movable-type.co.uk/scripts/latlong-db.html

我实际上已经在我的实时网站上运行了这个字符串,但是我现在正在重新运行运行MySQL 5.5.24的localhost(WAMP)上的一些工作。该平台是Wordpress。

我理解数据库名称,字段等的引号(或缺少引号),并且我使用了几种变体而没有运气。

无论如何,说得够多。这是错误:

[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 ''geo_data' WHERE 'Lat>44.566' AND 'Lat<44.566' AND 'Lon>-109.208' AND 'L' at line 4]

这是代码:

SELECT 'Postcode', 'Lat', 'Lon', 'acos(sin($lat)*sin(radians(Lat)) + cos($lat)*cos(radians(Lat))*cos(radians(Lon)-$lon))*$R' AS D
        FROM (
            SELECT 'Postcode', 'Lat', 'Lon'
            FROM 'geo_data'
            WHERE 'Lat>$minLat' AND 'Lat<$maxLat'
            AND 'Lon>$minLon' AND 'Lon<$maxLon'
        ) 
        AS 'firstcut' 
        WHERE 'acos(sin($lat)*sin(radians(Lat)) + cos($lat)*cos(radians(Lat))*cos(radians(Lon)-$lon))*$R' < '$rad'
        ORDER BY 'D'

感谢您的帮助,如果我错过了显而易见的话,请再次抱歉。

修改

谢谢你们!搞定了。这是为了他人利益的工作代码:

SELECT `Postcode`, `Lat`, `Lon`, acos(sin($lat)*sin(radians(`Lat`)) + cos($lat)*cos(radians(`Lat`))*cos(radians(`Lon`)-$lon))*$R AS `D`
        FROM (
            SELECT `Postcode`, `Lat`, `Lon`
            FROM `geo_data`
            WHERE `Lat`>'$minLat' AND `Lat`<'$maxLat'
            AND `Lon`>'$minLon' AND `Lon`<'$maxLon'
        ) 
        AS `firstcut` 
        WHERE acos(sin($lat)*sin(radians(`Lat`)) + cos($lat)*cos(radians(`Lat`))*cos(radians(`Lon`)-$lon))*$R < '$rad'
        ORDER BY `D`

2 个答案:

答案 0 :(得分:3)

您在字段和表名称周围使用单引号'。这是不正确的。您想使用反引号`。使用引号使MySQL将其解释为字符串。

你的条件和计算也在引号内,只是将它们变成字符串。

例如,您的内部查询应如下所示:

SELECT `Postcode`, `Lat`, `Lon`
FROM `geo_data`
WHERE `Lat > '$minLat' AND `Lat` < '$maxLat'
AND `Lon`> '$minLon' AND `Lon` < '$maxLon'

答案 1 :(得分:0)

'geo_data'是一个字符串geo_data [带反引号(`)]是一个表名