Reputation: 3754
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).
Upvotes: 0
Views: 56
Reputation: 116140
With UNION ALL
, you can select the parents first, then all the children. In the two unioned queries you can select extra information to help you sort. In this case, I introduce a type (1 is parent, 2 is child), and an orderdatetime
, which is basically the datetime of the parent in case of a child.
SELECT
x.id,
x.datetime
FROM
(SELECT
p.id,
p.datetime,
1 AS itemtype,
p.id as sortparent,
p.datetime AS sortdatetime
FROM
YourTable p
WHERE
p.parent IS NULL
UNION ALL
SELECT
c.id,
c.datetime,
2 AS itemtype,
c.parent as sortparent,
p.datetime AS sortdatetime
FROM
YourTable p
INNER JOIN YourTable c ON c.parent = p.id) x
ORDER BY
x.sortdatetime,
x.sortparent
x.itemtype,
x.datetime
For the sorting, I sort:
Upvotes: 1
Reputation: 21
Could save 'datetime' as time()
?
+----+--------+------------------------+
| id | parent | datetime |
+----+--------+------------------------+
| 1 | null | 1388615716 |
| 2 | 1 | 1388697666 |
| 4 | 2 | 1388790487 |
| 3 | 1 | 1388952461 |
+----+--------+------------------------+
And sort through ORDER BY datetime
.
Upvotes: 0