MySQL-使用BETWEEN搜索负经度值 [英] MySQL - Searching negative longitude values with BETWEEN
问题描述
我发现了许多类似的文章,甚至试图找出如何在MySQL中处理负值,但无济于事.
I have found many similar posts and even tried to find out how to handle negative values within MySQL, but to no avail.
我有一个正在使用Google Maps的网站,并且为了提高性能,我将在地图上绘制的标记限制为地图边界内的标记.
I have a site that I'm using Google Maps on and as a performance enhancement I'm limiting the markers that are drawn on the map to those that are within the map boundaries.
我想开发一个查询,该查询可以使用正或负latitude
和longitude
值.
I would like to develop a query that will work with positive or negative latitude
and longitude
values.
对于数据库:
latitude FLOAT( 10, 6 )
longitude FLOAT( 10, 6 )
查询:
SELECT *
FROM `table`
WHERE `latitude` BETWEEN 47.926930 AND 47.929806
AND `longitude` BETWEEN -97.077303 AND -97.083997
如果我删除longitude
的BETWEEN
子句,我会得到结果,尽管不正确,但没有longitude
约束.
If I drop the BETWEEN
clause for longitude
I get results, albeit incorrect with no longitude
constraint.
我已经尝试过:
AND -`longitude` BETWEEN ABS( -97.077303 ) AND ABS( -97.083997 )
这确实有效,但仅适用于负longitude
值.
Which does work, but only for negative longitude
values.
我是否需要检查longitude
是否为阴性?
Do I need to check longitude
if its negative?
推荐答案
您还可以使用greatest()
和least()
函数,因此您不必担心参数.例如:
You can also use the greatest()
and least()
functions, so you don't have to worry about the parameters. For example:
SELECT * FROM table
WHERE latitude BETWEEN least(@lat1, @lat2) AND greatest(@lat1, @lat2)
AND longitude BETWEEN least(@lon1, @lon2) AND greatest(@lon1, @lon2)
这篇关于MySQL-使用BETWEEN搜索负经度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!