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 'WHERE b.datasca < '2017-11-18' ORDER BY b.id_struttura ASC, b.datasca DESC' at line 9