I'm having a few issues with a mysql query. The table structure is the following :
+----+--------+---------------------+
| id | parent | datetime |
+----+--------+---------------------+
| 1 | null | 2014-03-01 09:14:02 |
| 2 | 1 | 2014-03-01 09:38:32 |
| 3 | 1 | 2014-03-01 09:45:52 |
| 4 | 2 | 2014-03-01 09:42:23 |
| 5 | null | 2014-03-01 09:47:42 |
| 6 | null | 2014-03-01 09:33:01 |
| 7 | 5 | 2014-03-01 09:54:39 |
+----+--------+---------------------+
I would like a 2 level result with parents ordered by datetime and in between if any children ordered by datetime. The data would to be listed as follows :
+----+--------+------------------------+
| id | parent | datetime |
+----+--------+------------------------+
| 1 | null | 2014-03-01 09:14:02 |
| 2 | 1 | 2014-03-01 09:38:32 |
| 4 | 2 | 2014-03-01 09:42:23 |
| 3 | 1 | 2014-03-01 09:45:52 |
| 5 | null | 2014-03-01 09:47:42 |
| 7 | 5 | 2014-03-01 09:54:39 |
| 6 | null | 2014-03-01 09:33:01 |
+----+--------+------------------------+
I can order parents by datetime, with the children in between but not by datetime (the children).