[GIS] How to find points inside each polygon in Mysql

MySQLpoint-in-polygonqueryspatial-database

I have points table (with latitude and longitude columns) and polygons table with geometry stored in Mysql. I want to get points inside one polygon, for example in polygon which has column name="California".

This is my query which returns all points inside all polygons, but I need all points inside polygon with name 'California':

$sql = "SELECT points.name FROM polygons, points WHERE ST_CONTAINS(polygons.geom, Point(points.longitude, points.latitude));"

Best Answer

$sql = "SELECT points.name FROM polygons, points WHERE ST_CONTAINS(polygons.geom, Point(points.longitude, points.latitude)) AND polygons.name = 'California'";