Database is mariadb. So this is my table and i want to sort it by ‘id’ descending but with the children always below the parent.
+----+-----------+--------------+--------+----------+
| id | firstName | favoriteFood | status | parentID |
+----+-----------+--------------+--------+----------+
| 1 | Andy | pizza | child | 5 |
| 2 | Alice | burger | child | 5 |
| 3 | Bob | fries | orphan | null |
| 4 | Barney | salad | parent | null |
| 5 | Christ | steak | parent | null |
| 6 | Daniel | pizza | child | 8 |
| 7 | Mike | fries | child | 5 |
| 8 | Richard | oatmeal | parent | null |
| 9 | Wilson | steak | child | 8 |
| 10 | Dicky | watermelon | orphan | null |
| 11 | Freya | potato | orphan | null |
| 12 | Ryan | oyster | parent | null |
| 13 | Alex | bread | orphan | null |
| 14 | Sarah | brocoli | child | 12 |
| 15 | Dane | toast | child | 8 |
+----+-----------+--------------+--------+----------+
I tried with this but it doesnt give my expected result
SELECT * from table ORDER BY id DESC, FIELD(status,'parent','child'), parentID
and this my expected result if its possible:
+----+-----------+--------------+--------+----------+
| id | firstName | favoriteFood | status | parentID |
+----+-----------+--------------+--------+----------+
| 13 | Alex | bread | orphan | null |
| 12 | Ryan | oyster | parent | null |
| 14 | Sarah | brocoli | child | 12 |
| 11 | Freya | potato | orphan | null |
| 10 | Dicky | watermelon | orphan | null |
| 8 | Richard | oatmeal | parent | null |
| 15 | Dane | toast | child | 8 |
| 9 | Wilson | steak | child | 8 |
| 6 | Daniel | pizza | child | 8 |
| 5 | Christ | steak | parent | null |
| 7 | Mike | fries | child | 5 |
| 2 | Alice | burger | child | 5 |
| 1 | Andy | pizza | child | 5 |
| 4 | Barney | salad | parent | null |
| 3 | Bob | fries | orphan | null |
+----+-----------+--------------+--------+----------+