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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:39:03+00:00 2026-05-16T07:39:03+00:00

Assume I have a table that has the following structure: ================= | Id |

  • 0

Assume I have a table that has the following structure:

=================
| Id | ParentId |
=================
| 1  | 0        |
| 2  | 1        |
| 3  | 1        |
| 4  | 2        |
| 5  | 2        |
| 6  | 3        |
| 7  | 3        |
-----------------

This results in a tree like this:

     1
    / \
  2     3
 / \   / \
4   5 6   7

Given an id, how do I get all the leaf nodes? So, if the given id is 2, the return should be 4 & 5. The given id will never be a leaf node itself.

I’m not sure how to modify the SQL here: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

SELECT t1.name FROM
category AS t1 LEFT JOIN category as t2
ON t1.category_id = t2.parent
WHERE t2.category_id IS NULL;

EDIT1: Also, how do I get the root id for a given id? So, if the given id is 2, the return should be 1.

  • 1 1 Answer
  • 3 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-05-16T07:39:04+00:00Added an answer on May 16, 2026 at 7:39 am

    “Also, how do I get the root id for a given id? So, if the given id is 2, the return should be 1.”

    That’s pretty easy, that’s the ParentId!

    You’ll get the children with the following query:

    SELECT child.Id FROM theTable current LEFT JOIN theTable child ON child.ParentId = current.Id;
    

    Btw, I wouldn’t recommend using 0 when the row has no parent, I’d rather use NULL in that case.

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

Sidebar

Related Questions

Using this as a simplified example, assume I have a table that has some
I have a city table that has two columns from_city to_city now lets assume
Assume I have a table like this: column A(int) column B(int) 1 2 2
i have the following table structure table location has - id - parentLocation_id (relation
Greetings, I have a table that looks like the following: date value 2007-11-05 134
I have this homework question: Assume a Database table has 0 records at time
Assume I have the following style table, col1 col2 and col3 have same value
I have a join table with the following structure: CREATE TABLE adjectives_friends ( adjective_id
I have a base table of Participants and Answers, with the following structure: ParticipantId,
I have a structure that looks following Class TreeNode { public TreeNode Parent {

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.