user1514444
user1514444

Reputation:

Parent - child sql query with in a single table

I've a table:

Id | Name |  ParentID

I want to select records whose parentid not equal to zero, with parent record(parent record have parentid = 0, but some parent record don't have child record I want to skip them )

Upvotes: 2

Views: 13799

Answers (2)

Shashika Silva
Shashika Silva

Reputation: 55

Check this one:

select * from child c,parent p where c.ID=P.ParentID and c.ParentID !=0 

Upvotes: 0

Igor Korkhov
Igor Korkhov

Reputation: 8558

Try this:

SELECT child.Id,
       child.Name,
       child.ParentId,
       parent.Name as ParentName
FROM your_table child
JOIN your_table parent ON child.ParentId = parent.id;

Upvotes: 3

Related Questions