BETWEEN不能正常工作

时间:2016-09-06 07:51:07

标签: mysql sql geocoding

我的表的纬度和经度值存储为文本。对于lat和long值为:

的记录
+------+-------------------+--------------------+
| Name |        Lat        |        Long        |
+------+-------------------+--------------------+
| ABC  | 32.74943951828427 | -97.33141142455626 |
+------+-------------------+--------------------+

我的SQL查询就像:

SELECT * FROM Incident I WHERE
(I.Lat BETWEEN '32.740446302225' AND '32.758432734343') 
AND 
(I.Long BETWEEN '-97.342104340547' AND '-97.320718508566')
LIMIT 5

但这导致返回0行。 "ABC"记录确实存在于表中,但我不知道它为什么不拉它。字符串不能作为浮点数进行比较吗?

1 个答案:

答案 0 :(得分:4)

这是因为您将它们用作字符串。

BETWEEN语法是 - > value BETWEEN small_val AND big_val,但由于您将它们用作字符串,因此-97.34..大于-97.32,因此无效。

尝试:

(CAST(I.Lat as UNSIGNED) BETWEEN 32.740446302225 AND 32.758432734343) 
AND 
(CAST(I.Long as UNSIGNED) BETWEEN -97.342104340547 AND -97.320718508566)

在String类型列中存储数值是错误的!我强烈建议您将其更改为数字类型,因为它应该是。