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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:34:13+00:00 2026-06-06T03:34:13+00:00

Suppose I have this table parent | child 1 2 1 3 2 4

  • 0

Suppose I have this table

parent | child 
   1       2        
   1       3        
   2       4        
   4       5        
   5       6        

and I want to check if 6 is a descendant of 1 (which it is)….can I accomplish this within one single query, or will I have to inevitably get all data and process them with PHP?

  • 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-06T03:34:15+00:00Added an answer on June 6, 2026 at 3:34 am

    You can walk the tree using recursion in MySQL without processing the data in PHP. MySQL doesn’t support true recursion though, so you’d have to settle for some recursive-ish techniques:

    One method is described in this SO question, but would involve changing your tables.

    Another method would be by using a stored function, which will loop through the table until the job is done (which is recursive in a way). This would probably work with your current table, but is more complex to achieve.

    I have an example right here that walks a TYPO3 pages table, finding all page ID’s for a given rootline (comparable to what you’re trying to achieve):

    BEGIN
            DECLARE _id INT;
            DECLARE _parent INT;
            DECLARE _next INT;
            DECLARE CONTINUE HANDLER FOR NOT FOUND SET @id = NULL;
    
            SET _parent = @id;
            SET _id = -1;
    
            IF @id IS NULL THEN
                    RETURN NULL;
            END IF;
    
            LOOP
                    SELECT  MIN(uid)
                    INTO    @id
                    FROM    pages
                    WHERE   pid = _parent
                    AND uid > _id
                    AND deleted = 0
                    AND hidden =0;                
                    IF @id IS NOT NULL OR _parent = @start_with THEN
                            SET @level = @level + 1;
                            RETURN @id;
                    END IF;
                    SET @level := @level - 1;
                    SELECT  uid, pid
                    INTO    _id, _parent
                    FROM    pages
                    WHERE   uid = _parent
                    AND     deleted = 0
                    AND     hidden = 0;
            END LOOP;
    END
    

    Excuse me for not having the time right now to tailor this example to your needs, making this an incomplete answer, but I’m hoping this can get you on the right track nonetheless.

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

Sidebar

Related Questions

suppose I have this table var 10 3 4 60 70 and I want
Suppose i have this table table (a,b,c,d). Datatypes are not important. I want to
Suppose I have a table with parent-child relationships. parent child 1 4 1 5
Suppose I have table with two columns. I can center this table by using:
Let's suppose I have this MySQL table: id place_id area_mq n -- -------- -------
Suppose you have a table like this: id user score It's a dump of
Suppose this markup: <table class=table table-bordered align=center> No mather how many cells I have,
Suppose I have this simple contract which I've taken from MS examples and altered
Suppose I have this class: .MyClass{background:red;} This class applies to several divs. I want
Suppose I have a table like so: <table> <tr><td class=this-is-a-label>Label Cell</td></tr> <tr><td>Detail 1</td></tr> <tr><td

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.