Click here to Skip to main content
15,850,685 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Every one,

I have a Following table data

SQL
UserId   ParentId  Name and Other Field

1               3           abc

2               1           abc

3               2          abc

4               1          abc

5               1           abc 



I have a Following Detail UserId = 5 AND ParentId = 2

Than I want Following Data.

SQL
UserId                ParentId                       Name

 1                             3                               abc

 3                             2                                abc 



Please help me and Comment Sql query that can get my desire output.


Thanks in Advance..
Posted
Comments
Richard Deeming 19-Sep-14 8:20am    
The data you've posted is not a hierarchy; you have no root node, and circular references.

For example, 3 is a child of 2, 2 is a child of 1, and 1 is a child of 3.

1 solution

Hi,

as per your info provided, check this...


SQL
SELECT UserId, ParentId,name  from yourTable WHERE UserId in
(
 SELECT ParentId from yourTable where UserId=5
)
union
SELECT * from yourTable where ParentId=2


Hope this will give your required output.


Cheers
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900