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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:15:11+00:00 2026-05-27T04:15:11+00:00

I have a table which I do a self-join. My sample table looks like

  • 0

I have a table which I do a self-join. My sample table looks like this

-- Table genealogy
+--------+---------+------+-----+---------+-------+
| Field  | Type    | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+-------+
| parent | int(11) | NO   | PRI | NULL    |       |
| child  | int(11) | NO   | PRI | NULL    |       |
+--------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)

The concept goes like this (get ready for this one):

  1. To find the child of user parent=1, I look at the first level of user parent=1.
  2. To find the grandchild of user parent=1, I look for the child who is the child of the child of user parent=1.
  3. To get the great-grandchild (3rd level), I look for the child who is the child who is the child of the child of parent=1.
  4. …and so forth

It can get a little confusing but my query works just fine. I was wondering if there’s a much faster/optimized way of doing it since I’m only repeating my sql all over the place.

-- Get Child
SELECT parent, child AS '1st Level' FROM genealogy WHERE parent=1;

-- Get Grandchild
SELECT a.parent, b.child AS '2nd Level' FROM (SELECT * FROM genealogy WHERE parent=1) a INNER JOIN genealogy b ON a.child=b.parent;

-- Get Great-Grandchild
SELECT a.parent, b.child AS '3rd Level' FROM (SELECT a.parent, b.child FROM (SELECT * FROM genealogy WHERE parent=1) a INNER JOIN genealogy b ON a.child=b.parent) a INNER JOIN genealogy b ON a.child=b.parent;

-- And the list goes on
  • 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-27T04:15:11+00:00Added an answer on May 27, 2026 at 4:15 am

    You could use another tree structure (called nested set):

    -- Table genealogy
    +--------+---------+------+-----+---------+-------+
    | Field  | Type    | Null | Key | Default | Extra |
    +--------+---------+------+-----+---------+-------+
    | id     | int(11) | NO   | PRI | NULL    |       |
    | lft    | int(11) | NO   | NO  | NULL    |       |
    | rgt    | int(11) | NO   | NO  | NULL    |       |
    +--------+---------+------+-----+---------+-------+
    

    This would enable you to select ALL childs (and grand childs and grand-grand-childs and so on) of a node inside the tree.

    For example to select all children of genealogy id 1:

    SELECT g2.*
    FROM genealogy AS g1, genealogy AS g2
    WHERE 
      (g1.lft BETWEEN g2.lft AND g2.rgt)
    AND 
      (g1.id = 1);
    

    You may have a look at this for more information and more examples: http://www.ibase.ru/devinfo/DBMSTrees/sqltrees.html

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

Sidebar

Related Questions

I have table data which looks like this id | keyword | count |
I have a table which is full of arbitrarily formatted phone numbers, like this
I have self join table. This table is being used to join up to
Is this any possible? I'd like to have something like User.avatar.to_url which would then
I have a query which uses a self join on 2 tables to return
I have a currency(Rec_ID, currency) table which is having M-M relationship with it self
I have a table of records, which has a self-relationship. Additionally - to make
I have a table which has self relationship: id -- parentId columns I need
I have a table which is referenced by foreign keys on many other tables.
I have a table which contains my server status create table ServerStatus ( ServerId

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.