I have two tables Post (id, ..., creationdate) and Post_Has_Post(parent_id, child_id).
Some Posts can be part of a parent<->child relation and have a corresponding entry in the Post_Has_Post-Table.
What I’m trying to do is getting a list of posts ordered by their creationdate. Any children of other posts however should be inserted after their parent in the result.
Now of course it’s easy to order by creationdate, but i’m out of ideas for the second sorting condition. Is there a way to do that at all?
Post Post_Has_Post
+------+-------+--------------+ +-----------+----------+
| id | ... | creationdate | | parent_id | child_id |
+------+-------+--------------+ +-----------+----------+
| 1 | ... | 2010-11-01 | | 1 | 3 |
| 2 | ... | 2010-11-02 | +-----------+----------+
| 3 | ... | 2010-11-03 |
+------+-------+--------------+
I need a result sorted like this:
Post
+------+-------+--------------+
| id | ... | creationdate |
+------+-------+--------------+
| 1 | ... | 2010-11-01 |
| 3 | ... | 2010-11-03 |
| 2 | ... | 2010-11-02 |
+------+-------+--------------+
Is there a way to solve this through sorting?
Assuming the parent always comes before the child, this should work.