SQL Query Parent-Child data within same table.
-
Hi... All I've database table named : tblCategory. The Table's structure is like : ChildID-------Category Name---------ParentID 1------------ Category 1 ---------- 0 2------------ Category 2 ---------- 1 3------------ Category 3 ---------- 1 4------------ Category 4 ---------- 2 5------------ Category 5 ---------- 3 6------------ Category 6 ---------- 4 Here, For Example : 'Category 4' is the Child Record of 'Category 2' because 'Category 4's ParentID is 2. Now, I want to find the all child record from ParentID. Like : If I entered simple ParentID 1, then I want the all nested child record of Parent 1. So, If Possble, Please give me sql query of it or any relavant solotuion. Thanks is advance.
Regards, Abhi
-
Hi... All I've database table named : tblCategory. The Table's structure is like : ChildID-------Category Name---------ParentID 1------------ Category 1 ---------- 0 2------------ Category 2 ---------- 1 3------------ Category 3 ---------- 1 4------------ Category 4 ---------- 2 5------------ Category 5 ---------- 3 6------------ Category 6 ---------- 4 Here, For Example : 'Category 4' is the Child Record of 'Category 2' because 'Category 4's ParentID is 2. Now, I want to find the all child record from ParentID. Like : If I entered simple ParentID 1, then I want the all nested child record of Parent 1. So, If Possble, Please give me sql query of it or any relavant solotuion. Thanks is advance.
Regards, Abhi
Just fire a query on ur datatable saying: DataRow[] foundParent ; foundParent = dt.Select("ParentID = 1"); if (foundParent.GetUpperBound(0) > 0) { for (Int32 intRowCnt = 0; intRowCnt <= foundParent.GetUpperBound(0); intRowCnt++) { foundChild = dt.Select("ParentID=" + foundParent[intRowCnt]["ChildID"]); if (foundChild.GetUpperBound(0) > 0) { //Do something } for (intColCnt = 0; intColCnt <= dt.Columns.Count - 1; intColCnt++) { //Do something } } and write this in for loop.
-
Just fire a query on ur datatable saying: DataRow[] foundParent ; foundParent = dt.Select("ParentID = 1"); if (foundParent.GetUpperBound(0) > 0) { for (Int32 intRowCnt = 0; intRowCnt <= foundParent.GetUpperBound(0); intRowCnt++) { foundChild = dt.Select("ParentID=" + foundParent[intRowCnt]["ChildID"]); if (foundChild.GetUpperBound(0) > 0) { //Do something } for (intColCnt = 0; intColCnt <= dt.Columns.Count - 1; intColCnt++) { //Do something } } and write this in for loop.
-
ASP.NET 2.0 wrote:
and write this in for loop.
Means, I have to write this whole code in for loop of each and every row of DataTable or this is enough ?? Thanks for your posting. Please, Reply of this my confusion. Thanks in advance.
Regards, Abhi
This is the code: DataRow[] foundParent ; foundParent = dt.Select("ParentID = 1"); if (foundParent.GetUpperBound(0) > 0) { for (Int32 intRowCnt = 0; intRowCnt <= foundParent.GetUpperBound(0); intRowCnt++) { foundChild = dt.Select("ParentID=" + foundParent[intRowCnt]["ChildID"]); if (foundChild.GetUpperBound(0) > 0) { //Do something } for (intColCnt = 0; intColCnt <= dt.Columns.Count - 1; intColCnt++) { //Do something } }
-
Hi... All I've database table named : tblCategory. The Table's structure is like : ChildID-------Category Name---------ParentID 1------------ Category 1 ---------- 0 2------------ Category 2 ---------- 1 3------------ Category 3 ---------- 1 4------------ Category 4 ---------- 2 5------------ Category 5 ---------- 3 6------------ Category 6 ---------- 4 Here, For Example : 'Category 4' is the Child Record of 'Category 2' because 'Category 4's ParentID is 2. Now, I want to find the all child record from ParentID. Like : If I entered simple ParentID 1, then I want the all nested child record of Parent 1. So, If Possble, Please give me sql query of it or any relavant solotuion. Thanks is advance.
Regards, Abhi
As Per Ur Requirement the query below is
select p.parentid,p.childname,p.childid from tblCategory , tblCategory p
where c.childid=p.parentid1 Category 2 2
1 Category 3 3
2 Category 4 4
3 Category 5 5
4 Category 6 6and also if u want to execute the query with where clause then
select p.parentid,p.childname,p.childid from #temp c , #temp p
where c.childid=p.parentid
and p.parentid=1Hope It help u ....:) Regards Tarini Singh Sr. Software Engineer. (IT) tksingh@zenta.com