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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:57:36+00:00 2026-06-17T01:57:36+00:00

The left table is a result of my query. And I need to sort

  • 0

enter image description here

The left table is a result of my query. And I need to sort it as the right table.

I need to order by p_id, if level >= 2. The blue box of right table is a target of order by.

Is it possible? Of course it is an example. Actual data is hundreds and really need to be sorted.

I searched a lot, but coudln’t find the same case.

edit : this table will be returned as java.util.ArrayList. If this kind of ‘order by’ is not possilbe, is it possible in java.util.ArrayList?

  • 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-17T01:57:37+00:00Added an answer on June 17, 2026 at 1:57 am

    I’m sure it’s not possible in one query in MySQL.

    In your diagram on the right, the ordering has been done in two separate steps:

    1. Sort by id

    2. Sort each block by p_id if level >= 2

    That’s quite difficult to do in MySQL as you would need to identify the blocks and then iterate over them, sorting each block separately.

    I’ve done something similar where ordering within blocks was required and then selecting from those ordered blocks. You can view that here but as I said, I think that that SQL code is horribly complicated involving 5 temporary tables. You would probably need fewer temp tables, but it would still be a very complicated procedure, quite slow and hard to maintain.

    “Actual data is hundreds and really need to be sorted.”

    Is there any reason why you can’t just sort it as you want in code?

    $blockStart = FALSE;
    $count = 0;
    
    foreach($dataArray as $data){
    
       if($blockStart === FALSE){
          $blockStart = $count;
       }
    
       if($data['level'] < 2){ //Block has finished
           sortBlock($dataArray, $blockStart, $count);
           $blockStart = $count;
       } 
    
        $count++;
    }
    
    sortBlock($dataArray, $blockStart, $count - 1);
    
    
    function sortBlock($dataArray, $indexStart, $indexEnd){
    
        //Sort the elements of $dataArray, between $indexStart and $indexEnd inclusive
        //by the value of p_id
    }
    

    Trying to solve a general programming problem in MySQL when you could solve it in 1/10th of the programmer time (and probably have it perform faster as well) in Java is not a good path to follow.

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

Sidebar

Related Questions

Is it possible to format output with write.table? I can left-align columns using tab,
I need to write an SQL-Query for a csv-export. I have a table stores,
I need to order some information with order by in a MySQL query. Is
I am doing a query where I need to strip out some lower-level results
I have a table names lcMovimientos and what I need is a query where
I want to left join 2 tables. So all attributes of left table should
I've seen people writing the following query SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2
Left outer joins should return at least one row from the T1 table if
The following table element in the center div causes the contents in the left
How do I align the table cell button to the left instead of the

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.