简体   繁体   中英

Writing a SQL query using postgis geometry on the same table

I'm fairly new to writing SQL queries, so thanks in advance. I have a postgres table called geo_areas. This table contains a list of cities, towns and states. I have a column called type which specifies whether it is a city or town or state. I also have a column called Geometry which holds the geometry of the said name. It looks similar to this,

Name, Type, Geometry

Philadelphia, Town, XXXXXXXXXX
Pennsylvania, State, XXXXXXXXXXX
Pittsburgh, Town, XXXXXXXXXXX
Porsche, City, XXXXXXXXX

I need to write a query that lists all the other names that is contained in it's geometry, similar to this.

Name, Type, Contains

Pennsylvania, State, [Porsche, Pittsburg, Philadelphia]
Porsche, City, [Philadelphia, Pittsburgh]
Philadelphia, Town, []
Pittsburgh, Town, []

Any help for a step in the right direction is appreciated, thanks!

assuming you want the geometry contained in a state you could try

select t2.* 
from  my_table t1  
inner join my_table t2 on st_dwithin(t1.geom,t2.geom, 0) 
and t1.type='State' 
and t2.type!='State' 

or for st_contains

select t2.* 
from  my_table t1  
inner join my_table t2 on st_contains(t2.geom,t1.geom) 
and t1.type='State' 
and t2.type!='State' 

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