MySQL - 在数字范围内找到最接近的匹配项

时间:2017-07-21 03:32:04

标签: mysql sql

我正在处理图表类型的应用程序,我希望在x,y轴开始到终点找到最佳解决方案。

MySQL数据:

enter image description here

说我有以下内容: starting_x:200 starting_y:150 ending_x:500 ending_y:605

所以我想在数据库中找到上述数字之间的最近匹配。

我的查询我现在正在使用:

SELECT * FROM `graph` ORDER BY `start_pos_x`,`start_pos_y`,`end_pos_x`,`end_pos_y` ASC 

我知道这与我尝试做的事情并不相近,但我很难在这里找到解决方案。

谢谢!

1 个答案:

答案 0 :(得分:1)

这似乎是一个算法问题。 首先,您必须定义the closest match between the above numbers to the database。例如,我可以将其定义为找到Xs和Ys之间差异的最小平方和。" 然后,解决方案可能是这样的:

Select 
  * 
From 
  `graph` 
Order by
  Power(starting_x-200, 2) + Power(starting_y-150, 2) 
  + Power(ending_x-500, 2) + Power(ending_y-605, 2) ASC
相关问题