Hi
I have a table with this structure
id integer
parent_id integer
order_n integer
info text
Each row could have a parent row (parent_id, null if doesn't have a parent), and an order of insertion (order_n). Every time a row is inserted, the order_n field will be set with the correlative "inside his parent". So, two rows of first level will be order_n = 1 and order_b = 2. But a new row "inside" row 1 will be order_n = 1
Example
id parent_id order_n info
1 null 1 "Beatles"
2 null 2 "Stones"
3 1 1 "Paul"
4 1 2 "John"
5 2 1 "Mick"
6 2 2 "Keith"
The sub-levels are infinite.
The thing I'm trying to do (and I fail miserably), is to make a query who retrieve all the rows for any level (including the first level), and order it according his order_n attribute, but grouping the nested rows. For example, in the previous example, we need to retrieve the results this way
1 null 1 "Beatles"
3 1 1 "Paul"
4 1 2 "John"
2 null 2 "Stones"
5 2 1 "Mick"
6 2 2 "Keith"
I'm trying and trying but I know very little about SQL, I will thanks in advance all your wise advice.
I'm using MySQL, but the ideal is try something "sql standard"
The inner levels are infinite.