我想选择与给定经度,纬度值最接近的位置 所以我创建了这个查询
SELECT *
FROM "Places"
WHERE 2 * atan2(
sqrt(
sin((radians(41.647780 - latitude))/2)*sin((radians(41.647780 - latitude))/2) +
sin((radians(25.295330 - longitude))/2)*sin((radians(25.295330 - longitude))/2) *
cos(radians(latitude)) * cos(radians(41.647780))
),
sqrt(1 -
sin((radians(41.647780 - latitude))/2)*sin((radians(41.647780 - latitude))/2) +
sin((radians(25.295330 - longitude))/2)*sin((radians(25.295330 - longitude))/2) *
cos(radians(latitude)) * cos(radians(41.647780))
)) * 6371 IS MINIMAL
LIMIT 1
主要的想法是这个公式返回所有点和我的硬编码之间的距离我在选择查询中尝试了公式并且它正在工作。
我希望得到“最小”距离,我需要像“IS MINIMAL”这样的东西。我知道上面的错误(也许是非常错误和愚蠢)但我在这些方面都是新手。我想我只需要添加或更改一两个字就可以了,但我不知道正确的。
答案 0 :(得分:1)
您要找的是ORDER BY .. LIMIT 1
:
SELECT *
FROM "Places"
ORDER BY atan2(
sqrt(
sin((radians(41.647780 - latitude))/2)*sin((radians(41.647780 - latitude))/2) +
sin((radians(25.295330 - longitude))/2)*sin((radians(25.295330 - longitude))/2) *
cos(radians(latitude)) * cos(radians(41.647780))
),
sqrt(1 -
sin((radians(41.647780 - latitude))/2)*sin((radians(41.647780 - latitude))/2) +
sin((radians(25.295330 - longitude))/2)*sin((radians(25.295330 - longitude))/2) *
cos(radians(latitude)) * cos(radians(41.647780))
))
LIMIT 1;
您可能对PostGis或此相关问题感兴趣:
How can I get results from a JPA entity ordered by distance?