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. Database & SysAdmin
  3. Database
  4. Getting Children of Parent Within a table

Getting Children of Parent Within a table

Scheduled Pinned Locked Moved Database
questiondatabasedata-structureslearning
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.
  • M Offline
    M Offline
    May Thu san
    wrote on last edited by
    #1

    I have a table Named 'Roles' which contains 'ID' and 'ParentID' fields. Some Sample Data is ID ,Name ,ParentID 1 ,Admin ,0 2 ,Manager ,1 3 ,Front Office User ,1 4 ,Supervisor ,2 5 ,Inventory User ,0 6 ,Human Resource User ,4 I would like to extract data of Parent and all children. SELECT b.[ID] as 'Parent', a.[ID] as 'Role' FROM Roles a INNER JOIN Roles b ON a.ParentID = b.[ID] and a.ParentID=1 which can return only. ID ,ParentID 1 ,2 1 ,3 My wanted form is ParentID ,ID 1 ,2 1 ,3 2 ,4 4 ,6 Like an organization tree, the top parent level can access all lower level nodes. Sql 2005 support that feature. How can I access them in Sql2000 recursive function or store procedure? Thanks May Thu San.

    W 1 Reply Last reply
    0
    • M May Thu san

      I have a table Named 'Roles' which contains 'ID' and 'ParentID' fields. Some Sample Data is ID ,Name ,ParentID 1 ,Admin ,0 2 ,Manager ,1 3 ,Front Office User ,1 4 ,Supervisor ,2 5 ,Inventory User ,0 6 ,Human Resource User ,4 I would like to extract data of Parent and all children. SELECT b.[ID] as 'Parent', a.[ID] as 'Role' FROM Roles a INNER JOIN Roles b ON a.ParentID = b.[ID] and a.ParentID=1 which can return only. ID ,ParentID 1 ,2 1 ,3 My wanted form is ParentID ,ID 1 ,2 1 ,3 2 ,4 4 ,6 Like an organization tree, the top parent level can access all lower level nodes. Sql 2005 support that feature. How can I access them in Sql2000 recursive function or store procedure? Thanks May Thu San.

      W Offline
      W Offline
      WoutL
      wrote on last edited by
      #2

      I think you made a little mistake in your query. This should work: SELECT b.[ID] as 'Parent', a.[ID] as 'Role' FROM Roles a INNER JOIN Roles b ON a.ParentID = b.[ID]

      Wout Louwers

      M 1 Reply Last reply
      0
      • W WoutL

        I think you made a little mistake in your query. This should work: SELECT b.[ID] as 'Parent', a.[ID] as 'Role' FROM Roles a INNER JOIN Roles b ON a.ParentID = b.[ID]

        Wout Louwers

        M Offline
        M Offline
        May Thu san
        wrote on last edited by
        #3

        Sorry, My Query is SELECT b.[ID] as 'Parent', a.[ID] as 'Role' FROM Roles a INNER JOIN Roles b ON a.[ParentID] = b.[ID] and a.ParentID=1 Some Sample Data is ID ,Name ,ParentID 1 ,Admin ,0 2 ,Manager ,1 3 ,Front Office User ,1 4 ,Supervisor ,2 5 ,Inventory User ,0 6 ,Human Resource User ,4 The above query can only show the first two child of ID 1 (which are 2,3) and did not show the child of parent 2 and 3...etc. How can I get all children?

        C W 2 Replies Last reply
        0
        • M May Thu san

          Sorry, My Query is SELECT b.[ID] as 'Parent', a.[ID] as 'Role' FROM Roles a INNER JOIN Roles b ON a.[ParentID] = b.[ID] and a.ParentID=1 Some Sample Data is ID ,Name ,ParentID 1 ,Admin ,0 2 ,Manager ,1 3 ,Front Office User ,1 4 ,Supervisor ,2 5 ,Inventory User ,0 6 ,Human Resource User ,4 The above query can only show the first two child of ID 1 (which are 2,3) and did not show the child of parent 2 and 3...etc. How can I get all children?

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          May Thu san wrote:

          The above query can only show the first two child of ID 1 (which are 2,3) and did not show the child of parent 2 and 3...etc. How can I get all children?

          by removing the "and a.ParentID=1" from the query.


          Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

          1 Reply Last reply
          0
          • M May Thu san

            Sorry, My Query is SELECT b.[ID] as 'Parent', a.[ID] as 'Role' FROM Roles a INNER JOIN Roles b ON a.[ParentID] = b.[ID] and a.ParentID=1 Some Sample Data is ID ,Name ,ParentID 1 ,Admin ,0 2 ,Manager ,1 3 ,Front Office User ,1 4 ,Supervisor ,2 5 ,Inventory User ,0 6 ,Human Resource User ,4 The above query can only show the first two child of ID 1 (which are 2,3) and did not show the child of parent 2 and 3...etc. How can I get all children?

            W Offline
            W Offline
            WoutL
            wrote on last edited by
            #5

            I think I understand what you want now. But this cannot be easily solved whit the ParentID. This[^] article should help you.

            Wout Louwers

            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