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

发布于 2024-12-26 08:00:44 字数 640 浏览 2 评论 0原文

我在 mysql 表中有这个:

在此处输入图像描述

idbolag_id< /code> 是 intlatlngitudedouble

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

lngitude 查询: SELECT * FROM location_forslag WHERElngitude = 13.8461208

但是,如果我使用 lat 列,它确实返回结果:

lat查询: SELECT * FROM location_forslag WHERElat= 58.3902782

lngitude 列有什么问题?

I have this in a mysql table:

enter image description here

id and bolag_id are int. lat and lngitude are double.

If I use the the lngitude column, no results are returned:

lngitude Query: SELECT * FROM location_forslag WHERElngitude= 13.8461208

However, if I use the lat column, it does return results:

lat Query: SELECT * FROM location_forslag WHERElat= 58.3902782

What is the problem with the lngitude column?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

飞烟轻若梦 2025-01-02 08:00:44

使用 = 等于运算符比较浮点数通常不是一个好主意。

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

1度约112公里,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 值。

It is not generally a good idea to compare floating point numbers with = equals operator.

For your application, you need to consider how close you want the answer to be.

1 degree is about 112km, and 0.00001 degrees is about 1.1 metres (at the equator). Do you really want your application to say "not equal" if two points are different by 0.00000001 degrees = 1mm?

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

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

This will return points where lngitude is within @epsilon degrees of the desired value.
You should choose a value for epsilon which is appropriate to your application.

忆依然 2025-01-02 08:00:44

浮点令人恼火....

 WHERE ABS(lngitude - 13.8461208) < 0.00000005

Floating points are irritating....

 WHERE ABS(lngitude - 13.8461208) < 0.00000005
狼性发作 2025-01-02 08:00:44

将浮点数转换为小数以进行比较。我遇到了同样的问题并这样解决:

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 子句后的前 3 行。
希望有帮助。

Convert float to decimal for compare. I had the same problem and solved like this:

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

Look at the first 3 rows after the WHERE clausule.
Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文