derBrain
derBrain

Reputation: 183

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!!

Upvotes: 0

Views: 933

Answers (1)

Diogo Silva
Diogo Silva

Reputation: 311

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')
)

Upvotes: 1

Related Questions