You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AND d.date<'2018-07-16' GROUP BY e.sid ORDER BY d.date DESC LIMIT 640, 10' at line 1