I have a table menu which contains rows which refers to others rows in the same table via foreign keys.
Here the table:
+----+---------------+--------+
| id | title | parent |
+----+---------------+--------+
| 1 | Enseignements | NULL |
| 2 | Juments | 4 |
| 3 | Étalons | 4 |
| 4 | Animaux | NULL |
| 5 | Tarifs | 1 |
+----+---------------+--------+
I would like to group rows according to hierarchical and alphabetical order, like this:
+----+---------------+--------+
| id | title | parent |
+----+---------------+--------+
| 4 | Animaux | NULL |
| 3 | Étalons | 4 |
| 2 | Juments | 4 |
| 1 | Enseignements | NULL |
| 5 | Tarifs | 1 |
+----+---------------+--------+
I just managed to group items from the same branch. The sub-level items are sorted by title. In fact, I wish that all the first-level items are also sorted by title, like this:
+----+---------------+--------+
| id | title | parent |
+----+---------------+--------+
| 1 | Enseignements | NULL |
| 5 | Tarifs | 1 |
| 4 | Animaux | NULL |
| 3 | Étalons | 4 |
| 2 | Juments | 4 |
+----+---------------+--------+
With the code :
SELECT title, COALESCE(parent, id), parent
FROM menu
GROUP BY COALESCE(parent, id), title
How can I do this ?