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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:04:02+00:00 2026-06-18T01:04:02+00:00

I have this records in my table : id | title | sub —–+————-+——

  • 0

I have this records in my table :

id   |   title     |  sub
-----+-------------+------
1    | Parent 1    | 0
2    | Parent 2    | 0
3    | Sub 1-1     | 1
4    | Parent 3    | 0
5    | Sub 1-2     | 1
6    | Sub 2-1     | 2
7    | Parent 4    | 0

Now I want select this records from my table with one query like this :

id   |   title     |  sub
-----+-------------+------
1    | Parent 1    | 0
3    | Sub 1-1     | 1
5    | Sub 1-2     | 1
2    | Parent 2    | 0
6    | Sub 2-1     | 2
4    | Parent 3    | 0
7    | Parent 4    | 0

I want to sort my records by Parent and then childs.

How can I do this in mySQL ?

UPDATE:

I use this query :

SELECT a.*, 
       CASE WHEN SUB = 0 THEN ID ELSE SUB END expression
  FROM category a
 ORDER BY CASE WHEN SUB = 0 THEN ID ELSE SUB END, ID

And my data is :

id  | title     | sub 
----+---------------+-------
1   | Parent 1  | 0
2   | Parent 2  | 0
3   | Sub 1-1   | 7
4   | Parent 3  | 0
5   | Sub 1-2   | 4
6   | Sub 2-1   | 2
7   | Parent 4  | 0

The result is :

id  | title     | sub   | expression
----+-----------+-------+-----------
1   | Parent 1  | 0     | 1
2   | Parent 2  | 0     | 2
6   | Sub 2-1   | 2     | 2
4   | Parent 3  | 0     | 4
5   | Sub 1-2   | 4     | 4
3   | Sub 1-1   | 7     | 7
7   | Parent 4  | 0     | 7
  • 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-06-18T01:04:03+00:00Added an answer on June 18, 2026 at 1:04 am

    If there wouldn’t be more than 1 level, you could make an expression with CASE on your order by clause:

    SELECT a.*, 
           CASE WHEN SUB = 0 THEN ID ELSE SUB END expression
      FROM test a
     ORDER BY CASE WHEN SUB = 0 THEN ID ELSE SUB END, SUB, TITLE
    

    With that expression, it will always order by the parentId, which is the SUB column when populated or the ID column when it is not a child.

    Would result:

    ID  TITLE        SUB   EXPRESSION
    1   Parent 1       0          1
    3   Sub 1-1        1          1
    5   Sub 1-2        1          1
    2   Parent 2       0          2
    6   Sub 2-1        2          2
    4   Parent 3       0          4
    7   Parent 4       0          7
    

    It would still fail if any of the Child could be a parent of another record (Sub 1-2-1 for example).

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

Sidebar

Related Questions

I have these two different queries. This query pulls the records from posts table
I have db with this table (TableToDo): http://goo.gl/NlTEk I want display all records in
I have table with 300 000 records (MyISAM). I get record from this table
I have read many article about this one. I want to hear from you.
I have this table Test as Id, SomeValue, SomeText contains about a mill records,
I have a table that has records with a structure similar to this.. ID
I have this query that, due to the number of records, is taking several
I have written this code inside a servlet to delete certain records from three
I have this SQL Server query which I wrote to find the Movie title
how to query a table Store(ID,Title,Company) to get all the records where title contains

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.