每日一题-27(平面上的最近距离)

题27:

请你写一个SQL查询报告Point2D表中任意两点之间的最短距离。保留2位小数。
在这里插入图片描述
解题思路:
(1)power(x.y)或者pow(x.y)表示对x求y次方
(2)判定不同点:x坐标或者y坐标不同
(3)对所有点求距离,然后求最小值后四舍五入
代码如下:

select min(res) as shortest from
(
    select round(sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2)), 2) as res from point2D a, point2D b
    where not (a.x=b.x and a.y=b.y)
    order by a.x, a.y, b.x, b.y
) as t;

猜你喜欢

转载自blog.csdn.net/Txixi/article/details/121636280
今日推荐