2011-05-13 49 views
0

我在下面的代碼中收到令人沮喪的MySql語法錯誤。實際的錯誤是:Haversine公式中的MySQL語法錯誤

SQLSTATE[42000]: Syntax error or access violation: 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 ' JOIN storetable ON pricelist.storecode = storetable.storecode JOIN itemlist ON' at line 9"

附加信息:基於谷歌地圖的PHP/MySQL例如這段代碼:http://code.google.com/apis/maps/articles/phpsqlsearch.html

我能得到這個代碼工作正常之前,我嘗試添加的JOIN語句。

$query = sprintf("SELECT storetable.storeaddress, 
         storetable.storename, 
         storetable.lat, 
         storetable.lng, 
         (3959 * acos(cos(radians('%s')) * 
         cos(radians(storetable.lat)) *        cos(radians(storetable.lng) - radians('%s')) + 
         sin(radians('%s')) * sin(radians(storetable.lat)))) AS distance 
        FROM pricelist 
        HAVING distance < 25 
       ORDER BY distance 
        LIMIT 0 , 20, 
        JOIN storetable ON pricelist.storecode = storetable.storecode 
        JOIN itemlist ON pricelist.upccode = itemlist.upccode", 
       mysql_real_escape_string($latitude), 
       mysql_real_escape_string($longitude), 
       mysql_real_escape_string($latitude)); 
+0

我不知道MySQL的,但爲什麼你使用'ORDER BY'加入之前? – 2011-05-13 04:56:58

+0

@taspeotis:你是對的。正如OMG Ponies指出的那樣,ORDER BY在JOINS之後。 – 2011-05-13 05:44:20

回答

1

加入是在錯誤的地方 - 他們來了之後FROM子句中,前WHERE子句 - 使用:

$query = sprintf("SELECT storetable.storeaddress, 
         storetable.storename, 
         storetable.lat, 
         storetable.lng, 
         (3959 * acos(cos(radians('%s')) * 
         cos(radians(lat)) * cos(radians(lng) - radians('%s')) + 
         sin(radians('%s')) * sin(radians(lat)))) AS distance 
        FROM pricelist 
        JOIN storetable ON pricelist.storecode = storetable.storecode 
        JOIN itemlist ON pricelist.upccode = itemlist.upccode 
        HAVING distance < 25 
       ORDER BY distance 
        LIMIT 0, 20", 
       mysql_real_escape_string($latitude), 
       mysql_real_escape_string($longitude), 
       mysql_real_escape_string($latitude)); 
+0

感謝您向我展示JOIN的正確順序。 我收到語法錯誤: > SQLSTATE [42000]:語法錯誤或訪問衝突:1064您的SQL語法錯誤;請檢查與您的MySQL服務器版本相對應的手冊,以便在第13行的「LIMIT 0,0」附近使用正確的語法。 > 20之後的逗號可能不正確嗎? – 2011-05-13 05:42:08

+0

@Paul:錯誤消息與我發佈的查詢不符。然而,我刪除了LIMIT中的尾隨逗號。 – 2011-05-13 06:00:56

+0

當我在PhpAdmin中運行此查詢時,它工作正常。但是,當我在程序中運行它時,它不會。我忘了提及我正在使用Php PDO。這會有所作爲嗎? – 2011-05-13 18:20:03