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 3424538
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:29:10+00:00 2026-05-18T06:29:10+00:00

Simple question.. just can’t get the result set in the order I need :p

  • 0

Simple question.. just can’t get the result set in the order I need :p

I have a table “categories”

id    | name     | parent
1       apple      0
2       macintosh  1
3       atari      0
4       st         3
5       lisa       1

I’m trying to select get the following result set:

1   apple      0
5   lisa       1
2   macintosh  1
3   atari      0
4   st         3

So in other words I want all columns of all rows, where rows with parents are immediately after their parent row and all are sorted alphabetically.

parent a
 child a
 child b
parent b
 child a

The query I’m using now doesn’t correctly re-order the rows after their parents

SELECT a.*, b.* FROM categories a RIGHT JOIN categories b ON b.parent = a.id
  • 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-18T06:29:11+00:00Added an answer on May 18, 2026 at 6:29 am

    For a simple, perhaps suboptimally-scalable solution, I recommend hard-coding this with the maximum number of levels you will have:

    For 2 levels only:

    SELECT p2.name as `Parent name`, p1.*
    FROM categories p1
    LEFT JOIN categories p2 on p1.categories_id = p2.id
    

    You’re really asking about sorting, so I’d recommend generating a “path”-like string:
    (see below for sample output of this query)

    SELECT Concat(If(isnull(p2.name),"",Concat("/",p2.name)),"/",p1.name) as `generated path`, p2.name as `Parent name`, p1.*
    FROM categories p1
    LEFT JOIN categories p2 on p1.parent_id = p2.id
    order by `generated path`
    

    For 3 levels, though your data doesn’t have this yet — path omitted because it will get ugly 🙂

    SELECT p3.name as `Grandparent name`, p2.name as `Parent name`, p1.*
    FROM categories p1
    LEFT JOIN categories p2 on p1.categories_id = p2.id
    LEFT JOIN categories p3 on p2.categories_id = p3.id
    

    A more comprehensive solution for quickly selecting all items in a particular category at any level, which does require some work on all writes, is implementing a ‘right’ and ‘left’ numbering concept. But, further discussion on that is almost certainly going beyond the scope of what you’re asking. However, that’s the only good way in my experience to make this kind of self-referencing table very useful if it’s going to get big (maybe after 1000+ rows with 3 to 10 levels).

    Addendum: sample output from the second query:

    generated path         Parent name         id         name         parent_id
    ----------------------------------------------------------------------------
    /apple                                      1         apple                0
    /apple/lisa                  apple          5         lisa                 1
    /apple/mac                   apple          2         mac                  1
    /atari                                      3         atari                0
    /atari/st                    atari          4         st                   3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure this is a simple question, just can't seem to get it to
Ok this is embarrassingly simple of a question, but I just can't get this
Simple question. I use Visual studio and I've just installed subversion, how can i
I know is a rather simple question but I just can't find an appropriate
Apologies for the rather simple question, I just can't seem to find ANY good
This should be a simple question, but I just can't recall the relevant API.
This should be a simple question, but I just can't seem to figure it
Trying to get started with subsonic but have a simple question about stored procedures
This is just a simple question, and I can't find the answer in the
Hey I have a really simple question that needs more of just an explanation

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.