[GIS] How to write a MYSQL spatial query to find all polygons (rectangle) that contain any points in a table

MySQLpointpolygonsql

Something like:

SELECT * from polygons WHERE bounds CONTAINS(SELECT geom_point from points)

Can someone help me out with the syntax? Bounds will always be a rectangular polygon and everything is in lon/lat. I could just check that each points lat/lng is between the min/max lat/lng of each polygon – but I cannot figure out the syntax to get MYSQL to loop / check all polys vs all points.

Thanks in advance.

Best Answer

Given a polygons table with rectangular polygons, and a points table called points, both with geometry columns called "geom", this should return any polygons that contain any of the points:

SELECT polygons.id FROM polygons, points WHERE CONTAINS(polygons.geom, points.geom);