Skip to content

Commit 2013f98

Browse files
authoredDec 7, 2023
Update pgp-updel.md
1 parent eac2d65 commit 2013f98

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed
 

‎pgp-updel.md

+22-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Is LIMIT 1 needed?
2424

2525
## Delete
2626

27-
### Delete Lines Contained in Polygons
27+
### Delete Geometries Contained in Polygons
2828
<https://gis.stackexchange.com/questions/372549/delete-lines-within-polygon>
2929

3030
Use an `EXISTS` expression.
@@ -33,11 +33,28 @@ This is an efficient way when traversing a table by row (as in an `UPDATE`/`DELE
3333
or otherwise comparing against a pre-selection (e.g. of ids).
3434

3535
```sql
36-
DELETE FROM <lines> AS ln
37-
WHERE EXISTS (
36+
DELETE FROM <geom_tbl> AS g
37+
WHERE EXISTS (
38+
SELECT 1
39+
FROM <poly_tbl> AS p
40+
WHERE ST_Within(g.geom, p.geom)
41+
);
42+
```
43+
44+
### Delete Geometries NOT Contained in Polygons
45+
<https://gis.stackexchange.com/questions/471587/delete-points-in-in-a-certain-area-using-postgis>
46+
47+
Use an `NOT EXISTS` expression.
48+
`NOT EXISTS` terminates the subquery as soon as a single row satisfying the `ST_Within` condition is found.
49+
This is an efficient way when traversing a table by row (as in an `UPDATE`/`DELETE`),
50+
or otherwise comparing against a pre-selection (e.g. of ids).
51+
52+
```sql
53+
DELETE FROM <geom_tbl> AS g
54+
WHERE NOT EXISTS (
3855
SELECT 1
39-
FROM <poly> AS pl
40-
WHERE ST_Within(ln.geom, pl.geom)
56+
FROM <poly_tbl> AS p
57+
WHERE ST_Within(g.geom, p.geom)
4158
);
4259
```
4360

0 commit comments

Comments
 (0)
Please sign in to comment.