为什么这个sql查询没有返回任何比较浮点数的结果?

时间:2012-01-12 17:27:53

标签: mysql double

我在mysql表中有这个:

enter image description here

idbolag_idintlatlngitudedouble

如果我使用lngitude列,则不会返回任何结果:

lngitude查询: SELECT * FROM location_forslag WHERE lngitude = 13.8461208

但是,如果我使用lat列,则 会返回结果:

lat查询: SELECT * FROM location_forslag WHERE lat = 58.3902782

lngitude列有什么问题?

3 个答案:

答案 0 :(得分:9)

将浮点数与=等于运算符进行比较通常不是一个好主意。

对于您的应用程序,您需要考虑您希望答案的接近程度。

1度约112km,0.00001度约1.1米(赤道处)。你真的希望你的应用程序说"不等于"如果两个点相差0.00000001度= 1mm?

set @EPSLION = 0.00001  /* 1.1 metres at equator */

SELECT * FROM location_forslag 
WHERE `lngitude` >= 13.8461208 -@EPSILON 
AND `lngitude` <= 13.8461208 + @EPSILON

这将返回lngitude在所需值的@epsilon度范围内的点。 您应该选择适合您的应用程序的epsilon值。

答案 1 :(得分:4)

Floating points are irritating ....

 WHERE ABS(lngitude - 13.8461208) < 0.00000005

答案 2 :(得分:0)

将float转换为decimal进行比较。我有同样的问题并解决了这样的问题:

SELECT
    [dbo].[Story].[Longitude],
    [dbo].[Story].[Latitude],
    [dbo].[Story].[Location],
FROM
    [dbo].[Story],
    [dbo].[Places]
WHERE
    convert(decimal, [dbo].[Story].[Latitude]) = convert(decimal,  [dbo].[Places].[Latitude])
    and
    convert(decimal, [dbo].[Story].[Longitude]) = convert(decimal, [dbo].[Places].[Longitude])
    and
    [dbo].[Places].[Id] = @PlacesID 
    and
    [dbo].[Story].IsDraft = 0
ORDER BY
    [dbo].[Story].[Time] desc

查看WHERE clausule之后的前3行。 希望它有所帮助。