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