sql - Need all data from table -
this question has answer here:
- hierarchical queries in sql server 2005 6 answers
- hierarchical data in mysql 3 answers
please help.
i have 1 table called employee.
i want record table self join parentid
record like
parent
b child of a
c child of a
d child of c
f child of b
e parent
g child of e
h child of g
if put self join , put record parent , a,b,c not d , f
i want record parent a,b,c,d,e .
you'll need use recursive cte if there isn't set number of parent/child levels. assuming you're using sql server 2005 or greater, should you're looking for:
with cte ( select id, id parentid employee parentid null union select e.id, c.parentid employee e join cte c on e.parentid = c.id ) select id cte parentid = 'a'
btw -- results in a, b, c, d, , f -- not e. assume typo in post.
Comments
Post a Comment