I have db table with parent child relationship as:
NodeId NodeName ParentId ------------------------------ 1 Node1 0 2 Node2
You should look into using the Nested Set Model for parent-child relationships within an SQL database. It's much nicer than trying to store the parentID of records in the table like this, and makes queries like this much easier.