SELECT c.id, c.user_id, c.body, c.deep, c.lineage, c.parent_id, ( SELECT COUNT(*) FROM comment where comment.lineage LIKE (CONCAT(c.lineage,'%')) AND comment.lineage!=c.lineage) as replies FROM comment as c order by c.linea
The first list is all the fields that you need to select, with the prefix c , which is an alias later in the comment table.
A query in a query is a subquery that executes this query, which does similar, and combines .clineage with % (which is a wildcard). This subquery result is stored in replies .
Results are ordered on linea .
alex
source share