I know this question comes up often, but today I can\'t find the answer I\'m looking for. I have a table with this schema.
CREATE TABLE `comments` (
`id`
Parents are records with no parent_id.
Children have parent_id equal to the parent comment's id.
SELECT ...
FROM comments AS parent
LEFT JOIN comments AS child
ON child.parent_id = parent.id
WHERE parent.parent_id IS NULL
ORDER BY parent.id, child.id;
Note that the self-join should be an outer join so that you don't miss parent comments with no children.