I have a table with:

Can I with a single query organize it to show:

Thank you.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
Yes, you could do this bastardization:
I say it’s a bastardization because it requires a computation for every single row. It will not be performant. The better way to solve this problem would be to use a better datamodel. Instead of storing just parent ids, you can also store using a Nested Set Model. So basically your datamodel would look like:
Now, with that, your order would be reduced to:
Which would do the exact same thing. Give that nested set article a read. There are better ways to solve the problem than just parentid if you need to get data out either filtered or sorted…