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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:03:45+00:00 2026-06-05T22:03:45+00:00

I currently have this sql statement that I wrote and it works but it’s

  • 0

I currently have this sql statement that I wrote and it works but it’s not quite what I want. I’ve been working on it for hours but can’t seem to get any further.

select parent.id as parent_id, parent.subject as parent,s.id,s.subject from (select s.id, s.subject from subjects s where parent_id = 0) parent join subjects s on parent.id = s.parent_id order by parent.subject, s.subject

It’s grabbing all the subjects and ordering correctly but I also want to return the parent subject (parent_id = 0) at the top of each grouping. This is because some parents may not have subjects underneath but I still need to return them. Also the ordering is off when I try to do it as I want the parent first then it’s child subjects. Hope that makes sense but if not just ask.

Any help would be appreciated.

Thanks

Steve

  • 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-05T22:03:46+00:00Added an answer on June 5, 2026 at 10:03 pm

    You’re talking about grouping sets of rows by their parent rows.

    The only way I know how to do this in MySQL is using the GROUP_CONCAT() function which won’t group the subjects by row, but rather create a grouped string.

    Here’s what you can do:

    SELECT
        a.id,
        a.subject,
        GROUP_CONCAT(CONCAT(b.id, ':::', b.subject) ORDER BY b.subject SEPARATOR '|||') AS subjectlist
    FROM
        subjects a
    LEFT JOIN
        subjects b ON a.id = b.parent_id
    WHERE
        a.parent_id = 0
    GROUP BY
        a.id,
        a.subject
    ORDER BY 
        a.subject
    

    So this will give you a result set like:

    id            |     subject     |     subjectlist
    ---------------------------------------------------------------------
    2             |     subj1       |     23:::childsubj1|||28:::childsubj4
    3             |     subj2       |     18:::childsubj8|||55:::childsubj16
    4             |     subj3       |     NULL
    

    Depending on what language you are using in your application, you may be able to “explode” the subjects string into arrays delimited first by ||| which separates each subject, then ::: which separates that subject’s ID and name.

    Obviously, the downside of this is you have to make sure that your child subject name does not contain either ||| or ::: or whichever delimiters you decide to use.

    Edit: Experimentally, I came up with this alternative solution which may be closer to what you’re looking for:

    Try:

    SELECT
        c.subj,
        c.id
    FROM
        (
            SELECT
                CONCAT('---> ', b.subject) AS subj,
                b.id,
                CONCAT(a.subject, b.subject) AS orderfactor
            FROM
                subjects a
            INNER JOIN
                subjects b ON a.id = b.parent_id
            WHERE
                a.parent_id = 0
    
            UNION ALL
    
            SELECT
                subject AS subj, 
                id, 
                subject AS orderfactor
            FROM
                subjects
            WHERE 
                parent_id = 0
        ) c
    ORDER BY
        c.orderfactor
    

    This query should give you a result along the lines of:

    subject                                 |       id       |
    ----------------------------------------------------------
    subj1                                   |       2        |
    ---> childsubj1                         |       23       |
    ---> childsubj4                         |       28       |
    subj2                                   |       3        |
    ---> childsubj8                         |       18       |
    ---> childsubj16                        |       55       |
    subj3                                   |       4        |
    subj4                                   |       5        |
    ---> childsubj10                        |       79       |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have this statement in my SQL View (SQL Server 2008 R2) which
I currently have this working but it requires me to have a static method
I'm currently debugging an Ms SQL Function (SQL 2008). In this function, I have
I currently have this useful code that I found elsewhere on StackOverflow: form.DrawToBitmap(bmp, new
I currently have a prepared statement in Java which uses the following SQL statement
I have a perplexing SQL select statement (ugly) that I'm trying to write in
I have a SQL Statement that I am trying to convert to a LINQ
I currently have a large SQL statement which i add the following line to
Currently i have this procedure under package that gets user_name when some selects user_id
I've been using SQL for years now but rarely anything more that simple inserts

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.