Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PostgreSQL中循环行?_Postgresql_Loops_Rows_Postgis - Fatal编程技术网

如何在PostgreSQL中循环行?

如何在PostgreSQL中循环行?,postgresql,loops,rows,postgis,Postgresql,Loops,Rows,Postgis,我有两个表:测试线和测试顶点 还有下面的查询,它根据test_顶点选择test_线的一部分。如果我的两个表包含一行,则查询工作正常。现在我想对多行使用查询。有没有一种简单的方法可以在不改变原始查询的情况下,对行进行两次循环并应用查询 WITH lines as (SELECT wkb_geometry from test_line as geom), points as (SELECT geom from test_vertex as geom), numgeoms (pt_count)

我有两个表:测试线和测试顶点

还有下面的查询,它根据test_顶点选择test_线的一部分。如果我的两个表包含一行,则查询工作正常。现在我想对多行使用查询。有没有一种简单的方法可以在不改变原始查询的情况下,对行进行两次循环并应用查询

 WITH
 lines as (SELECT wkb_geometry from test_line as geom),
 points as (SELECT geom from test_vertex as geom),
 numgeoms (pt_count) as (SELECT st_numgeometries(geom) + 1 as pt_count FROM points),
 knife_points as (
   SELECT x as segment, CASE WHEN x = 0 then 0
     WHEN x = (select pt_count from numgeoms) THEN 1 ELSE
     ST_Line_Locate_Point(l.wkb_geometry, ST_GeometryN(p.geom,x)) END as line_fraction_end,
     CASE WHEN x = 1 THEN 0  else
     ST_Line_Locate_Point(l.wkb_geometry, ST_GeometryN(p.geom,x-1)) END as line_fraction_start
   FROM points p, lines l, (SELECT generate_series(0, (SELECT pt_count from numgeoms)) as x ) g),
   segments as
    (SELECT ST_Line_Substring(wkb_geometry, line_fraction_start, line_fraction_end) as geom, segment
     FROM knife_points, lines WHERE segment >0 )
   SELECT geom, segment from segments
   WHERE ST_GeometryType(geom) = 'ST_LineString';

您需要定义多行的确切来源,以及test_线和test_顶点的关联和连接方式,除非您希望所有可能的组合都进行交叉连接。和往常一样,请给我你的博士后版本。基本的表定义和示例数据几乎是我们所希望的。无论如何,您都不需要循环。你几乎不需要一个带有正确SQL的循环。我知道这个查询:D我想你需要在ST_与a.geom、b.geom相交的地方添加一个循环,也可能需要一个GROUP BY。