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 '%20) GROUP BY dl.pid HAVING COUNT(dl.pid) = 1) dl2 ON dl2.pid = c.id WHERE c.ac' at line 1