PostGIS 递归方法

在Oracle数据库中,有可以实现递归的函数

select * from table_name
start with [condition1]
connect by [condition2]

最近发现Postgresql数据库也有类型递归查询的方法,

与Oracle不同的是,Postgresql没有现成的关键字表示层级和路径,需要自己构造(如下path和depath)

#前提数据必须是树状结构,首尾互联,方向一致(下例中,source和target互联)

WITH RECURSIVE t (gid,source,point,target,path,depath) AS ( 
SELECT t1.gid,t1.source,ST_StartPoint(t1.geom),t1.target,Array[t1.gid],1 as depath FROM zy t1 WHERE source = '8764' 
UNION ALL 
SELECT t2.gid, t2.source,ST_EndPoint(t2.geom),t2.target,t.path||t2.gid,t.depath+1 as depath FROM zy t2,t 
WHERE t2.source = t.target
)
SELECT * FROM t 

PostGIS 递归方法_第1张图片

 

你可能感兴趣的:(PostGIS)