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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:33:24+00:00 2026-06-12T17:33:24+00:00

I know this kind questions has been posted here many times, for exmaple: Java

  • 0

I know this kind questions has been posted here many times, for exmaple: Java way

I have huge amount of data (150k+) in standard tree pattern (id, parent_id, some_data)

Question: How to get leaves for given node_id?

Table structure:

CREATE TABLE `DATA_TREE` (
  `ID` int(11) NOT NULL,
  `PARENT_ID` int(11) NOT NULL,
  `DATA` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`ID`),
  UNIQUE KEY `ID_UNIQUE` (`ID`),
  KEY `fk_DATA_TREE_1_idx` (`PARENT_ID`),
  CONSTRAINT `fk_DATA_TREE_1` FOREIGN KEY (`PARENT_ID`) REFERENCES `DATA_TREE` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf

Database: MySQL 5.1.61

  • 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-12T17:33:25+00:00Added an answer on June 12, 2026 at 5:33 pm

    There is no way to do this in a single query. Even if there was it would probably be very inefficient.

    We can do it with a stored procedure and a loop. With the indexes you added it should be pretty quick too. This uses two tables selecting the nodes from the input table (A) and inserting the node and their children into (B). It then swaps B for A, and repeats until no more non-leaf nodes exist in A. The nice thing is loop iterations would only be as many as their are levels between the input node and the last leaf node, which under most cases is probably not that deep. This stored procedure would be faster than doing it externally in code.

    FYI I had difficulty with my installation handling temporary tables, if you get a ‘error 2’ then remove the temporary keyword.

    delimiter $$
    drop procedure if exists GetLeafNodes $$
    create procedure GetLeafNodes(nodeid int)
    begin
    declare N int default 1;
    
    -- create two working sets of IDs, we'll go back and forth between these two sets
    drop temporary table if exists A;
    drop temporary table if exists B;
    create temporary table A(node int, child int);
    create temporary table B(node int, child int);
    
    -- insert our single input node into the working set
    insert into A values (null, nodeid);
    
    while (N>0) do
      -- keep selecting child nodes for each node we are now tracking
      -- leaf nodes will end up with the child set to null
      insert into B
      select ifnull(A.child,A.node), tree.ID
        from A
        left outer join DATA_TREE as tree on A.child=tree.parent_id;
    
      -- now swap A and B
      rename table A to temp, B to A, temp to B;
    
      -- remove non-leaf nodes from table B
      delete from B;
    
      -- exit when there are no longer any non-leaf nodes in A
      set N=(select count(*) from A where child is not null);
    end while;
    
    -- now output our list of leaf nodes
    select node from A;
    
    drop temporary table A;
    drop temporary table B;
    end $$
    DELIMITER ;
    call GetLeafNodes(4);
    

    I used the following sample set for testing:

    CREATE TABLE `DATA_TREE` (
      `ID` int(11) NOT NULL,
      `PARENT_ID` int(11) NOT NULL,
      PRIMARY KEY (`ID`),
      UNIQUE KEY `ID_UNIQUE` (`ID`),
      KEY `fk_DATA_TREE_1_idx` (`PARENT_ID`)
    ) ENGINE=InnoDB
    ;
    
    insert into DATA_TREE values
    (1,0),(2,1),(3,1),(4,1),(5,3),(6,3),(7,4),(8,4),(9,4),(10,6),(11,6),(12,7),(13,9),(14,9),(15,12),(16,12),(17,12),(18,14);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this kind of questions have been asked already many times before. The
I know this kind of question has been asked many times. Yet I don't
I know this question has been kind of asked and have looked into the
I know this kind of question has been asked a few times, but alot
I know similar kind of question has been asked many times but seriously i
I know, this particular kind of thing has been answered a number of times,
I know this question has been asked a couple of times before. I m
I know this question has been asked here before, but I don't think those
I know that there has been a lot of questions like this but I
I know that the same question has been asked many times on SO and

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.