2015-04-01 143 views
-4

我想運行這個sql語句,但我得到一個錯誤,是什麼問題?我有一個名爲'zips'的表格來獲取所有符合條件的數據,我想要做的是鑄造LATLONGzips表格,它們是varchar類型。SQL語法錯誤#1064

SELECT * FROM zips 
     WHERE (
       CAST(LAT AS FLOAT) <= 41.009707145195 
       AND CAST(LAT AS FLOAT) >= 40.286092854805 
       AND CAST(LONG AS FLOAT) <= -73.307544588345 
       AND CAST(LONG AS FLOAT) >= -74.266455411655) 
       AND (
        CAST(LAT AS FLOAT) != 40.6479 
        AND CAST(LONG AS FLOAT) != -73.787 
        ) 
       ) 

,誤差

#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 'FLOAT) <= 41.009707145195 AND CAST(LAT AS FLOAT) >= 
40.286092854805 AND CAST(LON' at line 1 
+0

您至少有一個,也許是兩個括號問題 – 2015-04-01 20:46:00

+0

請更多解釋! @JohnConde – user3003810 2015-04-01 20:49:44

+0

LONG也是一個保留字http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html。在英語中有足夠多的單詞,您永遠不需要爲數據庫對象使用resrved字。 – 2015-04-01 20:50:54

回答

1

試試這個:

SELECT * FROM zips 
WHERE (
     CAST(LAT AS DECIMAL(10,6)) <= 41.009707145195 
     AND CAST(LAT AS DECIMAL(10,6)) >= 40.286092854805 
     AND CAST(`LONG` AS DECIMAL(10,6)) <= -73.307544588345 
     AND CAST(`LONG` AS DECIMAL(10,6)) >= -74.266455411655 
    ) 
     AND 
    (
     CAST(LAT AS DECIMAL(10,6)) != 40.6479 
     AND CAST(`LONG` AS DECIMAL(10,6)) != -73.787 
    ) 

LONG是保留關鍵字,以便把那裏面的backquotes

+0

#1064 - 您的SQL語法錯誤;檢查對應於您的MySQL服務器版本的手冊,以找到在'FLOAT'附近使用的正確語法<= 41.009707145195 AND CAST(LAT AS FLOAT)> = 40.286092854805 AND'在第3行 – user3003810 2015-04-01 20:56:39

+0

@ user3003810: - 更新了我的答案。請立即檢查! – 2015-04-01 20:59:37

+0

太棒了!完美的作品,謝謝,但你能解釋爲什麼浮動不工作! – user3003810 2015-04-01 21:01:33