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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:03:57+00:00 2026-06-12T18:03:57+00:00

I have a table like this- id name ordering catid 1 parent1 1 0

  • 0

I have a table like this-

id  name        ordering    catid 
1   parent1         1         0
2   parent2         2         0
3   parent3         3         0
4   child11         1         1
5   child12         2         1
6   child21         1         2
7   child22         2         2
8   child31         1         3
9   child32         2         3

I am trying to get the result like below-

id  name        ordering    catid 
1   parent1         1         0
4   child11         1         1
5   child12         2         1
2   parent2         2         0
6   child21         1         2
7   child22         2         2
3   parent3         3         0
8   child31         1         3
9   child32         2         3

I want to order the first parent(catid=0) than its child than second parent and its child.
Is this possible to achieve desire result with these column.I tried join but not succeeded.
Surely I am not doing it correctly.
This is what I have tried-

SELECT a.*,c.name AS category_title FROM table AS a 
LEFT JOIN table AS c ON c.id = a.catid 
ORDER BY c.ordering asc, a.ordering asc
  • 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-12T18:03:58+00:00Added an answer on June 12, 2026 at 6:03 pm

    This query would do a SELF JOIN on the same table, by joining the records from the first table with the corresponding parent records. The column parentId would contain the parent Id of the record if the parent existed, otherwise the ID of the record itself. The results are then ordered by parentId and then by the ID so that the parent always appears at the top in a given group of parent and its children.

    SELECT
        m.id, 
        m.name, 
        m.catid, 
        m.ordering, 
        p.ordering,
        case 
            WHEN p.ordering IS NULL THEN m.ordering * 10
            ELSE m.ordering + p.ordering * 10
        END AS parentId
    FROM
        MyTable m
        LEFT JOIN MyTable p
        ON m.catid = p.id
    ORDER BY parentId
    

    The result is as follows:

    1   parent1 0   1       10
    4   child11 1   1   1   11
    5   child12 1   2   1   12
    2   parent2 0   2       20
    6   child21 2   1   2   21
    7   child22 2   2   2   22
    3   parent3 0   3       30
    8   child31 3   1   3   31
    9   child32 3   2   3   32
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like this: name code group john 12 smith 15 how
Let's say I have a table like this: name |order_id ======================= first_record | 0
I have a table like this ID Name IsDeleted 1 Yogesh Null 2 Goldy
I have a table in mysql like this Name Result T1_09_03_2010 fail T2_09_03_2010 pass
I have a table that looks like this: Name Job Year ImpFile ImpDate ------------------------------------------------
Possible Duplicate: Concatenate row values T-SQL I have a table like this: ref_num name
I have a table that looks like this: table name: uno str_id | title
i have a table like this one: -------------------------------- id | name -------------------------------- 1 |
I have a table like this (simplified): ID | Name | Parent --------------------------------- 1
I have a table like this: Name State Amount ------------------------------ Pump 1 Present 339

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.