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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:34:07+00:00 2026-05-22T22:34:07+00:00

I have an application that uses a nested set model class to organise my

  • 0

I have an application that uses a nested set model class to organise my data, however I’m trying to write a query that will group_concat my results. I know I need to put some sub select statements somewhere but I can’t figure it out!

Here’s my structure at the moment:

table: person

 -----------+------------+-----------
|Person_ID  | Name       | Age       |
 -----------+------------+-----------
| 1         | Mark Vance | 19        |
| 2         | Michael Tsu| 22        |
| 3         | Mark Jones | 29        |
| 4         | Sara Young | 25        |
 -----------+------------+-----------

table: person_to_group

 ----+------------+-----------
|ID  | Person_ID  | Group_ID  |
 ----+------------+-----------
| 1  | 3          | 1         |
| 2  | 3          | 2         |
| 3  | 1          | 2         |
| 4  | 4          | 3         |
 ----+------------+-----------

table: groups

 ----------+--------------+--------------+-------------
|Group_ID  | Group_Name   | Group_Left   | Group_Right |
 ----------+--------------+--------------+-------------
| 1        | Root         | 1            | 6           |
| 2        | Node         | 2            | 5           |
| 3        | Sub Node     | 3            | 4           |
 ----------+--------------+--------------+-------------

I need to render something like this with my results:

//Grab the group_IDs for this person and put them in the class tag…

<li class="2 3">Sara Young is in the Sub Node Group</li>

Notice that although Sara is in the Sub Node group, she is still being given the id for Node aswell because she is a child of Node.

The following is the query that I am working with as a starting point.

SELECT *, GROUP_CONCAT( CAST( gg.Group_ID AS CHAR ) SEPARATOR ' ' ) Group_IDs
        FROM groups gg
        LEFT JOIN person_to_group AS t1 ON gg.Group_ID = t1.Group_ID
        LEFT JOIN person AS t2 ON t2.Person_ID = t1.Person_ID
        GROUP BY t2.per_ID 
        ORDER BY t2.Name ASC

Any help would be much appreciated!

  • 1 1 Answer
  • 1 View
  • 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-05-22T22:34:07+00:00Added an answer on May 22, 2026 at 10:34 pm

    Here’s how I’d write the query:

    SELECT p.Name, 
      GROUP_CONCAT( g.Group_Name ) AS Group_List,
      GROUP_CONCAT( CAST( gg.Group_ID AS CHAR ) SEPARATOR ' ' ) AS Group_ID_List
    FROM person AS p
    INNER JOIN person_to_group AS pg ON p.Person_ID = pg.Person_ID
    INNER JOIN groups AS g ON pg.Group_ID = g.Group_ID
    INNER JOIN groups AS gg ON g.Group_Left BETWEEN gg.Group_Left AND gg.Group_Right
    GROUP BY p.Name
    ORDER BY p.Name ASC
    

    Note that if you group by person name, you also need to GROUP_CONCAT the list of group names. According to your schema, a person could belong to multiple groups, because of the many-to-many relationship.

    I also recommend against using SELECT * in general. Just specify the columns you need.

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

Sidebar

Related Questions

I have an application that uses a nested set model class to organise my
I have an application that uses a ResourceDictionary to set the styles, which it
I have an application that uses multiple WebViews. Nowhere do I set the priority
I have an application that uses CoreData. I previously had a class named Marker
I have an application that uses Entity Framework to access the data, and throughout
I have an application that uses the bluetooth to receive some data (bytes) from
I have client application that uses WCF service to insert some data to backend
I have an application that uses the accelerometer. Sometimes, the application will launch without
I have application that uses direct data access to it's Database. I have nothing
I have an application that uses a UITableView to present data that I have

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.