table:
id parent_id
--------------
a_1 NULL
a_2 NULL
b_1 a_1
c_1 b_1
d_1 c_1
Now, I have id:d_1
How can I get a_1,b_1,c_1 in one SQL query? (d_1‘s ancestor ids)?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
I really don’t think you can do this in a simple SQL statement, it’s a fundamental limitation of the SQL language.
Here is a heinous stored procedure that does roughly what you want (for SQL Server). So you would need to do something like this.