Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3221228
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:51:08+00:00 2026-05-17T15:51:08+00:00

I have a table with this structure id integer parent_id integer order_n integer info

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:51:08+00:00Added an answer on May 17, 2026 at 3:51 pm

    It’s not a hard query to write until your very last line “The inner levels are infinite.”. You will be joining the table onto itself once for every level you need. If you had a predefined maximum of 5 levels, you could join the table to itself 5 times (left joins) to accomplish that.

    very psuedo code:

    Select whatever
    from mytable my1
    left join mytable my2 on my1.id = my2.parent_id
    where whatever
    order by case when parent_id is null then id else parent_id end, 
    case when parent_id  is null then 0 else order_n end
    

    The case statements in the order by clause are designed to identify the top parent record and group them with the rest.

    Want more levels? Expanding the join statement:
    left join mytable my3 on my2.id = my3.parent_id
    left join mytable my4 on my3.id = my4.parent_id
    left join mytable my5 on my4.id = my5.parent_id

    without a ‘maximum’ number of levles, dynamic SQL as per Matthew PK is (as far as I know) your only recourse.

    my1,my2,my3, etc might not be the easiest alias naming convention either, pick something you can follow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have already googled for this I have a Table with following structure in
This is the MySQL table structure that I have: itemID (BIGINT) userID (BIGINT) wasAdded
I have 2 tables: posts tags Tags table is structured like this: post_id tag
Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this
I have this table in an Oracle DB which has a primary key defined
I have a table like this: <table> <tfoot> <tr><td>footer</td></tr> </tfoot> <tbody> <tr><td>Body 1</td></tr> <tr><td>Body
I have a table like this (Oracle, 10) Account Bookdate Amount 1 20080101 100
Say I have this table: Person table -------------- PersonId Address table ------------ AddressId PersonAddressId
If I have a table like this: CREATE TABLE sizes ( name ENUM('small', 'medium',
I have a table for a contact form and this table contains another table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.