Skip to content
Advertisement

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 )

Advertisement

Answer

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;
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement