I have a flat list of pages in a mySQL Table.
ID | Tab index | Page Name | parent
Pages can be children of another page, specified by the “parent” property.
The whole list is sorted using the “Tab index” int column.
Can anybody think of a way to query this list so that
-
Items are ordered by Tabindex
-
Items that are children of another item are grouped behind the parent item, and ordered by their tabindices (optional)?
My mySQL knowledge doesn’t reach that deep.
This is an existing data structure and can’t be changed.
Thanks in advance for any input.
Assuming columns id, pagename, tabindex, parent_id
will return those columns in “ti” order which is the first non-null value of the following:
This lets you leave child tabindexes blank and have them “inherit” from the parent. Also, if you want the default sort value to push to the bottom, you can replace the 0 (third arg in coalesce) with (select max(tabindex) + 1 from page). The only caveat is that if a child tabindex is larger than the following parent, it will appear later in the list.