为什么 MySQL 找不到已存在的记录。这涉及经纬度数据库搜索。

huangapple go评论112阅读模式
英文:

Why Mysql does not find records that exists. It is about Lat&Lng database search

问题

Mysql无法找到已存在的记录可能的原因是什么。这与Lat&Lng数据库搜索有关。我有这个简单的查询:

select id from addresses where lat = "42.5632172" and lng = "27.5266380"

这个查询返回0个结果,但很明显记录是存在的。以下是我的数据库结构和记录:

为什么 MySQL 找不到已存在的记录。这涉及经纬度数据库搜索。
为什么 MySQL 找不到已存在的记录。这涉及经纬度数据库搜索。

提前感谢您的时间!

英文:

what could possible be the reason for Mysql does not find records that exists. It is about Lat&Lng database search. I have this simple query:

select id from addresses where lat = "42.5632172" and lng = "27.5266380" 

which returns 0 results but its obvious that record exists.
Here is my database structure and records:
为什么 MySQL 找不到已存在的记录。这涉及经纬度数据库搜索。
为什么 MySQL 找不到已存在的记录。这涉及经纬度数据库搜索。

Thanks in advance for your time!

答案1

得分: 2

由于您的latlng列的数据类型是float,这不是一个精确的数据类型,因此在这种数据类型上进行比较通常会导致意外的结果。最好在比较这些float列的值时使用一些容差。

SELECT id
FROM addresses
WHERE ABS(lat - 42.5632172) <= 0.0000001
      AND ABS(lng - 27.5266380) <= 0.0000001

但如果您能将列的数据类型更改为精确数字数据类型,例如DECIMAL,那将更好。

查看演示这里

英文:

Since data type of your lat and lng columns is float which is not a precision data type, so comparing on this data type often lead to unexpected result. It's better to use some tolerance when comparing values of these float columns.

 SELECT id 
 FROM addresses 
 WHERE ABS(lat - 42.5632172) &lt;= 0.0000001 
       AND ABS(lng - 27.5266380) &lt;= 0.0000001

But it's much better if you could change data type of your columns to precision number data type, such as DECIMAL.

See demo here.

huangapple
  • 本文由 发表于 2023年2月19日 17:03:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定