Closed Thread
Results 1 to 5 of 5

Thread: Finding All Children

  1. #1
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Finding All Children

    Okay so lets say my table looks like this:
    Code:
    id,   name, parent
     1,   root, null
     2, group1, 1
     3, group2, 1
     4, group3, 1
     5, group4, 2
     6, group4, 8
    So let's say if I know the root's ID number, it's really easy to get that ID and then immediately all it's children
    SELECT * FROM `table` WHERE `parent`=1
    That will return group1,group2, and group3. But now lets say I want to get root's children and their children recursively. Is there a way to do this with a single query? Or would I have to do a script to get kids and for each group do another query until I run out of kids?

    Maybe you could propose another table structure to help efficiency.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: Finding All Children

    I find your table structure fine as it is. hard to make something more normalized. I'll go with the digging thing in a script, everything depending on how you want the data in the end, how do you want it? are you building a tree structure, or do you want them flat as soon as you got them from the tree?
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

  4. #3
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Finding All Children

    I'm really just trying to find the most efficient way of telling if "group4" is a child of "group1"

    If the chain goes really far down (100+ nodes) then that would require a query for each level and would obviously take up a lot of resources.

  5. #4
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: Finding All Children

    hmm...
    i dont know if theres a way to do it in a single query, i would handle it with a little help from php
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  6. #5
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: Finding All Children

    I would do this in php yes. but 100 levels doesn't take that huge amount of resources anyway. it's not end of the world. but. you want to start with the child (if you know it) and climb the ladder instead of doing it up-to-down. this way, you climb more limited, until parent == 0, as you can't know if a row is parent or not.
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Does Opensource Really Help with Finding a Job?
    By Liars_paradox in forum The Lounge
    Replies: 4
    Last Post: 01-03-2011, 02:50 PM
  2. Replies: 21
    Last Post: 03-28-2010, 09:38 PM
  3. Loose coupling and parents/children/related
    By RedShift in forum General Programming
    Replies: 4
    Last Post: 12-06-2008, 10:10 PM
  4. Finding a DLL
    By Cosmet in forum C and C++
    Replies: 1
    Last Post: 02-13-2007, 09:04 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts