leetcode613. 直线上的最近距离(SQL)

表 point 保存了一些点在 x 轴上的坐标,这些坐标都是整数。

写一个查询语句,找到这些点中最近两个点之间的距离。

| x   |
|-----|
| -1  |
| 0   |
| 2   |
 

最近距离显然是 '1' ,是点 '-1' 和 '0' 之间的距离。所以输出应该如下:

| shortest|
|---------|
| 1       |
 

注意:每个点都与其他点坐标不同,表 table 不会有重复坐标出现。

select min(a.x-b.x) as 'shortest'
from point as a,point as b 
where a.x>b.x;
发布了589 篇原创文章 · 获赞 1万+ · 访问量 139万+

猜你喜欢

转载自blog.csdn.net/hebtu666/article/details/104396744