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 ') group by g.id, g.group order by g.group' at line 4 SELECT g.id, g.group, count(*) cnt from tag_groups as g inner join tags as t on g.id = t.groupx where 1=1 AND g.id in () group by g.id, g.group order by g.group