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
  • 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-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

I have an update query like following: update table TABLE1 set COL1 = 'X'
Hallo All, Please help in determining the best procedure for the following problem. In
I have two tables - incoming tours(id,name) and incoming_tours_cities(id_parrent, id_city) id in first table
The situation is akin to the following: Assume I am a store selling fruits,
I am working on a BlackBerry j2me Java implementation that does not have the
I need some help with Hibernate Projections. I have a class called Activity with
This question refers to the dequeueing of messages in Oracle Streams Advanced Queueing .
i'm trying to get my head around 2nd Level Caching in Fluent NHibernate. So
I'm dealing with sql and my sql is not as good as my ruby,
I am facing a problem for writing an sql query which should be easy

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.