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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:58:21+00:00 2026-06-01T19:58:21+00:00

I have a MySQL table like this : mysql> SELECT ID, Username, Parent, LeftNum,

  • 0

I have a MySQL table like this :

mysql> SELECT ID, Username, Parent, LeftNum, RightNum FROM usercopy;

+----+------------+----------+---------+----------+
| ID | Username   | Parent   | LeftNum | RightNum |
+----+------------+----------+---------+----------+
|  1 | root       |          |       1 |       92 |
|  2 | robson     | perta    |      62 |       89 |
|  3 | budi       | root     |       2 |       35 |
|  4 | bejo       | root     |      36 |       47 |
|  5 | ben        | puff     |      13 |       24 |
|  6 | boll       | root     |      48 |       91 |
|  7 | charlie    | robson   |      63 |       80 |
|  8 | charles    | robson   |      81 |       88 |
|  9 | doni       | budi     |       3 |       28 |
| 10 | dono       | budi     |      29 |       34 |
| 11 | donatello  | bejo     |      37 |       38 |
| 12 | donat      | bejo     |      39 |       42 |
| 13 | tello      | bejo     |      43 |       46 |
| 14 | pras       | ben      |      14 |       15 |
| 15 | prasetyo   | ben      |      16 |       21 |
| 16 | pram       | ben      |      22 |       23 |
| 17 | jeni       | boll     |      49 |       58 |
| 18 | cocala     | boll     |      59 |       60 |
| 19 | perta      | boll     |      61 |       90 |
| 20 | joko       | charlie  |      64 |       65 |
| 21 | aditya     | charlie  |      66 |       79 |
| 22 | mogot      | aditya   |      67 |       70 |
| 23 | arya       | aditya   |      71 |       78 |
| 24 | saiful     | mogot    |      68 |       69 |
| 25 | nur        | arya     |      72 |       77 |
| 26 | herlambang | nur      |      73 |       74 |
| 27 | barney     | nur      |      75 |       76 |
| 28 | conan      | charles  |      82 |       85 |
| 29 | john       | charles  |      86 |       87 |
| 30 | mark       | conan    |      83 |       84 |
| 31 | mike       | doni     |       4 |       11 |
| 32 | puff       | doni     |      12 |       25 |
| 33 | karti      | doni     |      26 |       27 |
| 34 | sukarto    | mike     |       5 |        6 |
| 35 | harto      | mike     |       7 |        8 |
| 36 | tien       | mike     |       9 |       10 |
| 37 | tono       | dono     |      30 |       31 |
| 38 | thomas     | dono     |      32 |       33 |
| 39 | dunkin     | donat    |      40 |       41 |
| 40 | jentol     | tello    |      44 |       45 |
| 41 | henri      | prasetyo |      17 |       18 |
| 42 | yani       | prasetyo |      19 |       20 |
| 43 | juli       | jeni     |      50 |       51 |
| 44 | rio        | jeni     |      52 |       53 |
| 45 | luke       | jeni     |      54 |       55 |
| 46 | harry      | jeni     |      56 |       57 |
+----+------------+----------+---------+----------+

46 rows in set (0.00 sec)

what I’m doing right now is showing group members based on his / her parent using PHP LOOP, which I hate it so bad. but, I have no idea how to JOIN / GROUP this MySQL table so that the output is created by single SQL command.

here’s what I do with PHP, for example my Username is ‘robson’ :

1. `SHOW any users with parent name = robson` 

mysql> SELECT ID, Username, Parent, LeftNum, RightNum FROM usercopy WHERE Parent = 'robson';

+----+----------+--------+---------+----------+
| ID | Username | Parent | LeftNum | RightNum |
+----+----------+--------+---------+----------+
|  7 | charlie  | robson |      63 |       80 |
|  8 | charles  | robson |      81 |       88 |
+----+----------+--------+---------+----------+

2 rows in set (0.00 sec)

 2. `SHOW Charlie's descendent` 

mysql> select ID, Username, Parent, LeftNum, RightNum from usercopy where LeftNum BETWEEN 63 AND 80;

+----+------------+---------+---------+----------+
| ID | Username   | Parent  | LeftNum | RightNum |
+----+------------+---------+---------+----------+
|  7 | charlie    | robson  |      63 |       80 |
| 20 | joko       | charlie |      64 |       65 |
| 21 | aditya     | charlie |      66 |       79 |
| 22 | mogot      | aditya  |      67 |       70 |
| 23 | arya       | aditya  |      71 |       78 |
| 24 | saiful     | mogot   |      68 |       69 |
| 25 | nur        | arya    |      72 |       77 |
| 26 | herlambang | nur     |      73 |       74 |
| 27 | barney     | nur     |      75 |       76 |
+----+------------+---------+---------+----------+

9 rows in set (0.00 sec)

3. LOOP Step 2 to SHOW Charles's descendent

mysql> select ID, Username, Parent, LeftNum, RightNum from usercopy where LeftNum BETWEEN 81 AND 88;

+----+----------+---------+---------+----------+
| ID | Username | Parent  | LeftNum | RightNum |
+----+----------+---------+---------+----------+
|  8 | charles  | robson  |      81 |       88 |
| 28 | conan    | charles |      82 |       85 |
| 29 | john     | charles |      86 |       87 |
| 30 | mark     | conan   |      83 |       84 |
+----+----------+---------+---------+----------+

4 rows in set (0.00 sec)

Now the question is any idea how to do do STEP 2, STEP 3, … , STEP X (based on how many descendent on Step 1) with single SQL command?

  • 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-01T19:58:22+00:00Added an answer on June 1, 2026 at 7:58 pm

    I think you would need a recursive query, which mysql doesn’t support. The way round it is to either use a different structure which stores the hierarchy or a stored procedure which mimics what you are doing in php.

    Bill Karwin talks about this kind of structure in his presentation on MySQL anti-patterns, which is pretty interesting for further study.

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

Sidebar

Related Questions

I have an MySQL query like this: SELECT MAX(messages.ID) as maxID, messages.from, messages.read, users.userName
I have a MySQL table like this: Payments +----+---------------------+---------+ | id | date (sorted
I have a MySQL table looking like this: > describe books; +---------------+--------------+------+-----+---------+----------------+ | Field
I have a MySQL table structured somewhat like this: type name value ===================== 1
I have a MySQL table that looks like this: `id` int(10) unsigned NOT NULL
I have this mysql table called comments which looks like this: commentID parentID type
I have a mySQL table that has a column like this: ID ----- 0352
Let's say I have a MySQL table that is something like this: software table:
Lets say I have a MySQL table, Users, like this - ----------------------------------------- ID |
I have a table in mysql like this Name Result T1_09_03_2010 fail T2_09_03_2010 pass

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.