I have a table, having a self-referencing parent-child relationship, as follows:
----------------------------------------
| Table: Menu |
----------------------------------------
| id | parent | name | ordinal |
----------------------------------------
| 1 | null | search | 1 |
---------------------------------------
| 2 | null | location | 2 |
----------------------------------------
| 3 | 1 | artifact | 1 |
----------------------------------------
| 4 | 2 | city | 1 |
----------------------------------------
| 5 | 2 | county | 2 |
----------------------------------------
I want to list items as parents first (according to their ordinals), then remaining children according to their ordinals. The dept of hierarchy is 1. But when I run following query, it lists only items that have parents (i.e. children only); excluding parents with null parents (i.e. root)
SELECT m FROM Menu m ORDER BY m.parent.ordinal, m.ordinal --> excludes root items
Is there a way to write something like this:
SELECT m FROM Menu m ORDER BY IF_EXISTS(m.parent.ordinal), m.ordinal --> should include root items too
Thank you.