SELECT a.id FROM (SELECT O1.lft, O1.id, COUNT(O2.id) FROM content_tree AS O1, content_tree AS O2 WHERE O1.lft BETWEEN O2.lft AND O2.rgt GROUP BY O2.id ORDER BY O1.lft; ) AS a MySQL said: #1064 - 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 '' at line 8