Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. SQL Query Parent-Child data within same table.

SQL Query Parent-Child data within same table.

Scheduled Pinned Locked Moved ASP.NET
databasetutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Abhi
    wrote on last edited by
    #1

    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

    V C 2 Replies Last reply
    0
    • A 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

      V Offline
      V Offline
      varshavmane
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • V varshavmane

        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.

        A Offline
        A Offline
        Abhi
        wrote on last edited by
        #3

        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

        V 1 Reply Last reply
        0
        • A Abhi

          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

          V Offline
          V Offline
          varshavmane
          wrote on last edited by
          #4

          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 } }

          1 Reply Last reply
          0
          • A 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

            C Offline
            C Offline
            codeproject_Tarun
            wrote on last edited by
            #5

            As Per Ur Requirement the query below is

            select p.parentid,p.childname,p.childid from tblCategory , tblCategory p
            where c.childid=p.parentid

            1 Category 2 2
            1 Category 3 3
            2 Category 4 4
            3 Category 5 5
            4 Category 6 6

            and 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=1

            Hope It help u ....:) Regards Tarini Singh Sr. Software Engineer. (IT) tksingh@zenta.com

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups