I have a Comments table like below, in MySQL:
content created_at id parent_id
"second comment", 2014-06-03T10:08:44+0000, 37, -1
"third comment", 2014-06-03T10:10:35+0000, 40, -1
"Under third", 2014-06-03T10:10:44+0000, 41, 40
"Under second", 2014-06-03T10:11:59+0000, 42, 37
Users can add new comments, which won't have a parent_id, as they are not children of other comments; users can also reply to comments that have been added through the previous method, so they are children of the primary comments, like on a second level of hierarchy. The parent_id column represents the id of a parent comment, if present. If the comment doesn't have a parent, default parent_id is -1.
That being said, I would like to query all the comments from the table, each parent followed by its children, ordered by created_at ASC. Example from the above data set:
second comment
Under second
third comment
Under third
I thought of using GROUP BY, as it resembles a grouping strategy, but not actually grouping all children into a single row. What would be a solution to this kind of query ? Are there more types of solutions ?