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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:22:20+00:00 2026-05-25T19:22:20+00:00

Using MySQL, I want to return a list of parents, from a table that

  • 0

Using MySQL, I want to return a list of parents, from a table that has a field structure like this. ID,PARENTID,NAME (a standard parent-child hierarchy). I would like to traverse “up” the tree to return a list of ALL ‘parents’.

I realize that “nested set”, might be a better way to handle this – but currently I cannot change the structure of the data. I will look to do that in the future. Currently – my set of data will realistically contain a few levels of depth – nothing crazy… maybe 2-5 so my recursive hit shouldn’t be ‘too expensive’.

I’ve looked at the solutions presented in SQL Server get parent list – but this syntax bombs in mySQL…

Does anyone have an example of how to do this?

@kevin – thx for link – but I still get error. (“every derived table must have it’s own alias”)

Here’s what I did (modified syntax form above article – to ‘fit’ MySQL) – I clearly missed something…

SELECT parents.*
FROM  (
    SELECT taskID,  task,  parentID,  0 as level
    FROM   tasks
    WHERE taskidID = 9147
    UNION ALL
    SELECT  taskID, task,  parentID,  Level + 1 
    FROM   tasks
    WHERE  taskID = (SELECT parentID FROM parents ORDER BY level DESC LIMIT 1)
    )

thoughts???

EXAMPLE:

ID      PARENTID    NAME
9146    0       thing1
9147    0       thing2
9148    9146        thing3
9149    9148        thing4
9150    0       thing5
9151    9149        thing6

Query for parents of “thing3”
Returns “9148,9146”

Query for parents of “thing6”
Returns “9149,9148,9146,0”

  • 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-25T19:22:20+00:00Added an answer on May 25, 2026 at 7:22 pm

    In this example we are checking 5 levels up:

    select 
        t1.parentid, t2.parentid, t3.parentid, t4.parentid, t5.parentid
    from
        tableName t1
        left join tableName t2 on t1.parentid = t2.id
        left join tableName t3 on t2.parentid = t3.id
        left join tableName t4 on t3.parentid = t4.id
        left join tableName t5 on t4.parentid = t5.id
    where
        t1.name = 'thing3'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using MySQL, i'm selecting a list of songs in spanish that i would like
I am using MySQL as DB and I want to retrieve table data to
OK i want to implement the searching feature using mysql. Now to do this,
Normally when I want to query large result set using Mysql I write this
I have two mysql queries that return a column of phone numbers. I want
I am using mssql and I want to make a query that returns a
I want to hide all mysql error messages without using mysql_error() . Are there
I'm using MySQL, and I want to do a sort of ternary statement in
I'm using MySQL to store Cyrillic and Latin strings. I want to display all
I am using mysql and facing some problem. I want to retrieve last row

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.