Parent/Child data

Discussion in 'OT Technology' started by Gonrad, Jul 24, 2008.

  1. Gonrad

    Gonrad OT Supporter

    Joined:
    Nov 15, 2003
    Messages:
    17,422
    Likes Received:
    0
    Location:
    Toronto
    This is the content of my table called "Category"

    Is this possible through a query alone or do I need to read ALL the data into memory and then recursively sort it all?

    This is in PHP+MySQL btw.

    Code:
    mysql> select id, name, parentid from category;
    +----+-----------+----------+
    | id | name      | parentid |
    +----+-----------+----------+
    |  1 | category1 |        0 |
    |  2 | category2 |        3 |
    |  3 | category3 |        0 |
    |  4 | category4 |        2 |
    |  5 | category5 |        0 |
    +----+-----------+----------+
    
    I need to be able to do an SQL query that lists the items in order. So I'd like to see something like this:

    Code:
    +----+-----------+----------+
    | id | name      | parentid |
    +----+-----------+----------+
    |  1 | category1 |        0 |
    |  3 | category3 |        0 |
    |  2 | category2 |        3 |
    |  4 | category4 |        2 |
    |  5 | category5 |        0 |
    +----+-----------+----------+
    
    Which visually would look like this:
    Code:
    - category1
    - category3
    - - category2
    - - - category4
    - category5
    
     
  2. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    What are you building, a form menu?
     
  3. EkriirkE

    EkriirkE Zika Xenu OT Supporter

    Joined:
    Jan 11, 2004
    Messages:
    14,799
    Likes Received:
    0
    Location:
    Dublin & San Francisco, CA
    What's your logic behind the sorting?
    for building the tree, though, you'll need to do some recursion
     

Share This Page