我有一张表,上面有点X
和Y
。我需要找到最接近原点的X
and点。Y
(0, 0)
我正在尝试这种方式:
SELECT *
FROM `line`
WHERE xi < yi and 0 < xi and 0 < yi and yi < xi
ORDER BY yi and xi ASC
Limit 100
但我没有得到想要的值。
到原点的距离由 给出sqrt(xi^2 +yi^2)
。由于平方根是严格升序的,您可以省略它以进行排序。这给出了:
SELECT *
FROM `line`
ORDER BY xi*xi + yi*yi
Limit 100
您需要计算距离d = sqrt(x²+y²)
以获取距原点最近的点
select x, y, sqrt(x*x + y*y) as distance
from `line`
order by distance asc
limit 1
SELECT xi, yi, xi * xi + yi * yi As r2
FROM line
ORDER BY r2