简体   繁体   中英

PostGIS distance query between 2 selected points from same table

This is probably a really easy query, but for some reason, I just can't figure it out.. I have a table with cities (cities) and I want to get the distance between London (cities.city_name='London') and Dundee. This is my query:

SELECT 
cities.city_name, ST_Distance(cities.the_geom,cities.the_geom)
from cities 
WHERE cities.city_name in ('London','Dundee')

The result is in the ST_Distance field of the output is 0 for both cities. I know that I'm missing something, but I don't know what. I assume I have to set aliases for the 2 cities, but I don't know how.

Can anybody help me out with this?

Thanks a ton in advance!!

You are comparing distance between the same point in your query:

ST_Distance(cities.the_geom,cities.the_geom) --compares the same column in the list of the two cities:

CITY   | GEOM  |  DISTANCE
-----------------------------------
London | GEOM1 | DISTANCE (GEOM1, GEOM1)
Dundee | GEOM2 | DISTANCE(GEOM2, GEOM2)

Hope you can see my point from table above

Maybe something like this:

SELECT ST_Distance(
    (SELECT the_geom FROM cities WHERE city_name ='London'),
    (SELECT the_geom FROM cities WHERE city_name ='Dundee')
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM