I'm stuck with the logic of a MySQL query where I need to find if a row exists where:
- The column longitude and latitude are within a Polyline
- The column hit is duplicated within the last month
I've been able to achieve the second point using :
select latitude,
longitude,
count(*)
from hits
WHERE datetime BETWEEN (CURRENT_DATE() - INTERVAL 1 MONTH) AND CURRENT_DATE()
group by latitude,
longitude
having count(*) > 1
but I can't figure out a way to only search for rows where the latitude and longitude are within a Polyline. Performance is a main factor here as the table hits can have millions of records.(can also do with a version 1 not so scalable/performant)
Any help would be much appreciated!
EDIT: I'm using MySQL 5.7 and I also have a column coordinates of type point (spatial type) also Polyline means :
A polyline is a list of points, where line segments are drawn between consecutive points
EDIT 2: Here is the query for people using php/symfony
$query = $this->em->createQuery('
SELECT count(i) as counter
FROM HitsBundle:Hit i
WHERE i.datetime BETWEEN :lastMonth AND :now
AND
MBRCovers(
ST_Buffer(
ST_GeomFromText(\'LineString('.$linestring.')\') ,
0.00001
),
i.coordinates
) = 1
GROUP BY i.coordinates
HAVING counter > 1
')
->setParameter('lastMonth', $lastMonth)
->setParameter('now', new \DateTime())
->setMaxResults(1);