简体   繁体   English

如何从mysql中的数据库中获取最接近的值

[英]how to get nearest value from database in mysql

I am using mySQL and CodeIgniter .我正在使用mySQLCodeIgniter I have some floating point numbers in my database such as我的数据库中有一些浮点数,例如

  • 8.3456 8.3456
  • 8.5555 8.5555
  • 4.5556 4.5556

I want to...我想要...

SELECT * FROM table WHERE value = $myvalue

but I can't use value = $myvalue in my SELECT query because $myvalue is not exactly equal to database values.但我不能在我的 SELECT 查询中使用value = $myvalue ,因为$myvalue不完全等于数据库值。 I need to get the nearest value to $myvalue from database.我需要从数据库中获取最接近$myvalue的值。

If $myvalue is 5 I want to select the value 4.5556 .如果$myvalue是 5 我想选择值4.5556

How can I do this in mySQL?如何在 mySQL 中执行此操作?

select * 
from table 
order by abs(value - $myvalue)
limit 1

Assuming that you have a 10% tolerance (+/-) you could try something like: 假设您有10%的容差(+/-),您可以尝试以下方法:

select * from table 
where value >= ($myvalue * .9) and value <= ($myvalue * 1.1) 
order by abs(value - $myvalue) limit 1

Slightly updated stealing from others - this should return the nearest result in the assumed tolerance range. 稍微更新其他人的窃取 - 这应该在假定的容差范围内返回最近的结果。 (Also, I just noticed the where was incorrect, apologies - now it should work). (另外,我只是注意到哪里不正确,道歉 - 现在它应该工作)。

(
select   *
from     table
where    value >= $myvalue
order by value asc
limit 1
)
union
(
select   *
from     table
where    value < $myvalue
order by value desc
limit 1
)
order by abs(value - $myvalue)
limit 1

This may look counter-intuitive but the speed will be greater than the other queries shown so far. 这可能看起来与直觉相反,但速度将大于目前为止显示的其他查询。

This is due to the fact that a greater than and less than query is quicker. 这是因为greater thanless than查询的速度更快。

Then doing an ABS on two values is nothing. 然后在两个值上做ABS就没什么了。

This will give you the quickest return in a single query I can think of. 这将为您提供我能想到的单个查询中最快的回报。

Doing an ABS on a whole table will be slow as it will scan the whole table. 在整个表上执行ABS会很慢,因为它会扫描整个表。

Get the largest value similar to $val: 获得类似于$ val的最大值:

SELECT * FROM tab WHERE val <= $val ORDER BY val DESC LIMIT 1

Get the smallest value similar to $val: 获得类似于$ val的最小值:

SELECT * FROM tab WHERE val >= $val ORDER BY val LIMIT 1

Get the closest value similar to $val in either direction: 在任一方向上获得与$ val类似的最接近值:

SELECT * FROM tab ORDER BY abs(val - $val) LIMIT 1

In my case, I was using the browsers geolocations and trying to find a closest city/state based on the coordinates I had in a table.就我而言,我正在使用浏览器的地理位置,并试图根据我在表格中的坐标找到最近的城市/州。

table structure:表结构:

id    zipcode    city_state   lat    lon
1     12345      Example, GA  85.3   -83.2

Recommend testing this vigorously before using -- probably needs some tweaks, but I came up with this as a start建议在使用前大力测试——可能需要一些调整,但我想出了这个作为开始

SELECT city_state, 
   zipcode, 
   ( Abs( lat - -33.867886 ) 
     + Abs( lon - -63.987) ) AS distance
FROM   zipcodes 
ORDER  BY distance 
LIMIT  1;  

For laravel users:对于 laravel 用户:

$city = Zipcodes::selectRaw
    ('city_state, zipcode,  ( ABS( lat - ? ) + ABS( lon - ?) ) AS distance', [$lat, $lon])
        ->orderBy('distance')
        ->first();

echo $city->city_state

Hope this helps someone someday.希望有一天这对某人有所帮助。

从以下内容中获取第一个值:

select * from table order by abs(value - $myvalue);
SELECT number, ABS( number - 2500 ) AS distance
FROM numbers
ORDER BY distance
LIMIT 6

Selecting closest values in MySQL 在MySQL中选择最接近的值

Try this: 试试这个:

SELECT *,abs((columnname -Yourvalue)) as near
  FROM table
 WHERE order by near limit 0,1
SELECT * FROM table1 ORDER BY ABS(value - '$myvalue') LIMIT 1 

Read this page http://dev.mysql.com/doc/refman/5.1/en/mathematical-functions.html#function_round 阅读此页http://dev.mysql.com/doc/refman/5.1/en/mathematical-functions.html#function_round

but your select would look like this 但你的选择会是这样的

select value from table where ROUND(value) = $myvalue 

Unfortunately, I think your database will probably do a full table scan for solutions that involve abs , so they will be (very) slow once your table grows. 不幸的是,我认为你的数据库可能会对涉及abs解决方案进行全表扫描,因此一旦你的表增长它们会非常慢。 A fast-running solution may be found in this earlier thread . 可以在此早期的线程中找到快速运行的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM