本文介绍了MySQL-带FLOAT(10,6)的BETWEEN运算符的行为类似于>和&lt ;,而不是> =和< =的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表,其中包含纬度和经度.我一直成功使用BETWEEN子句,直到遇到搜索的值与数据库中的值相同的情况.在这些情况下,它不会返回结果.这是一个示例,其中为:

I have a table with latitudes and longitudes of locations. I was using the BETWEEN clause successfully until I reached cases where the values being searched for where the same as those in the database. In these cases it is not returning results. Here is an example, where as:

SELECT 
  `Location`.`latitude`,
  `Location`.`longitude`
FROM
  `locations` AS `Location` 
WHERE `latitude` >= 40.735619 
  AND `latitude` <= 40.736561 
  AND `longitude` >= -74.033882 
  AND `longitude` <= -74.030861;

返回:

"latitude"  "longitude"
"40.736561" "-74.033882"
"40.735619" "-74.030861"

如果我使用BETWEEN条款(请注意,我什至尝试):

If I use the BETWEEN CLAUSE (Notice I've even tried this):

SELECT   
  `Location`.`latitude`,
  `Location`.`longitude`
FROM
  `locations` AS `Location` 
WHERE `latitude` BETWEEN LEAST(40.735619, 40.736561)
  AND GREATEST(40.736561, 40.735619)
  AND `longitude` BETWEEN LEAST(- 74.033882, - 74.030861)
  AND GREATEST(- 74.030861, - 74.033882)

我得到0条结果.哦,还有,如果我对每个值Ex加和/或减0.000001. "BETWEEN(40.735619-0.00001)"等.如果执行此操作,则确实会返回两个结果.

I get 0 results. Oh, whats more, if I add and/or subtract 0.000001 to each value Ex. "BETWEEN (40.735619-0.00001)" etc. If I do this it does return the two results.

好的,我将使用> =和< =,但是我不明白的是为什么BETWEEN表现为>和<在 docs 中,它非常清楚:

Fine, I'll use >= and <= but what I don't understand is why BETWEEN is acting like > and < when in the docs its pretty clear:

推荐答案

您应该使用小数数据类型而不是浮点数.浮点值的相等性,以及两者之间的不精确

You should use a decimal data type rather than a float. Equality, and hence between, for floating point values is imprecise

这篇关于MySQL-带FLOAT(10,6)的BETWEEN运算符的行为类似于&gt;和&lt ;,而不是&gt; =和&lt; =的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 10:13