Tree structure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    Tree structure


    I'm looking for some advice on how to design a set of tables to represent a
    tree style table structure in MySQL.

    I have a table that stores a set of items, with each item having a type. The
    type of the item belongs to a tree structure of types.

    e.g.

    Type1 +---- Type2 ---- Type3 +---- Type4
    | +---- Type5
    |
    +---- Type6 ---- Type7

    Type8 +---- Type9
    +---- Type10 +---- Type11
    +---- Type12

    I need to run queries on the item table to pull back all items that belong
    to a type or any of it's child types

    e.g.

    where type = Type2 or type = Type3 or type = Type4 or type = Type5
    or
    where type = Type10 or type = Type11 or type = Type12

    (I hope the diagram and psuedo code helps to explain what I'm trying to
    achieve :-))

    At the moment I store the structure of the type tree in an array within PHP
    and build up a long where clause but someone hinted that there might be a
    more efficient database approach to this - a cross reference table?
    Unfortunately I didn't get a chance to discuss it further with him.

    If anyone could suggest an approach or even point me in the general
    direction of some good info on the concept I'd much appreciate it,

    Steve


  • Steve

    #2
    Re: Tree structure

    Nevermind. I should have done some more searching first :-)

    If anyone is trying to do the same thing then they might want to check out
    this article:



    It has an excellent approach to it, starting from page 2.

    Steve


    "Steve" <me@removethisb it_stephenmcnab b.com> wrote in message
    news:402ff715$0 $7065$cc9e4d1f@ news-text.dial.pipex .com...[color=blue]
    >
    > I'm looking for some advice on how to design a set of tables to represent[/color]
    a[color=blue]
    > tree style table structure in MySQL.
    >
    > I have a table that stores a set of items, with each item having a type.[/color]
    The[color=blue]
    > type of the item belongs to a tree structure of types.
    >
    > e.g.
    >
    > Type1 +---- Type2 ---- Type3 +---- Type4
    > | +---- Type5
    > |
    > +---- Type6 ---- Type7
    >
    > Type8 +---- Type9
    > +---- Type10 +---- Type11
    > +---- Type12
    >
    > I need to run queries on the item table to pull back all items that belong
    > to a type or any of it's child types
    >
    > e.g.
    >
    > where type = Type2 or type = Type3 or type = Type4 or type = Type5
    > or
    > where type = Type10 or type = Type11 or type = Type12
    >
    > (I hope the diagram and psuedo code helps to explain what I'm trying to
    > achieve :-))
    >
    > At the moment I store the structure of the type tree in an array within[/color]
    PHP[color=blue]
    > and build up a long where clause but someone hinted that there might be a
    > more efficient database approach to this - a cross reference table?
    > Unfortunately I didn't get a chance to discuss it further with him.
    >
    > If anyone could suggest an approach or even point me in the general
    > direction of some good info on the concept I'd much appreciate it,
    >
    > Steve
    >
    >[/color]


    Comment

    • Steve

      #3
      Re: Tree structure

      Nevermind. I should have done some more searching first :-)

      If anyone is trying to do the same thing then they might want to check out
      this article:



      It has an excellent approach to it, starting from page 2.

      Steve


      "Steve" <me@removethisb it_stephenmcnab b.com> wrote in message
      news:402ff715$0 $7065$cc9e4d1f@ news-text.dial.pipex .com...[color=blue]
      >
      > I'm looking for some advice on how to design a set of tables to represent[/color]
      a[color=blue]
      > tree style table structure in MySQL.
      >
      > I have a table that stores a set of items, with each item having a type.[/color]
      The[color=blue]
      > type of the item belongs to a tree structure of types.
      >
      > e.g.
      >
      > Type1 +---- Type2 ---- Type3 +---- Type4
      > | +---- Type5
      > |
      > +---- Type6 ---- Type7
      >
      > Type8 +---- Type9
      > +---- Type10 +---- Type11
      > +---- Type12
      >
      > I need to run queries on the item table to pull back all items that belong
      > to a type or any of it's child types
      >
      > e.g.
      >
      > where type = Type2 or type = Type3 or type = Type4 or type = Type5
      > or
      > where type = Type10 or type = Type11 or type = Type12
      >
      > (I hope the diagram and psuedo code helps to explain what I'm trying to
      > achieve :-))
      >
      > At the moment I store the structure of the type tree in an array within[/color]
      PHP[color=blue]
      > and build up a long where clause but someone hinted that there might be a
      > more efficient database approach to this - a cross reference table?
      > Unfortunately I didn't get a chance to discuss it further with him.
      >
      > If anyone could suggest an approach or even point me in the general
      > direction of some good info on the concept I'd much appreciate it,
      >
      > Steve
      >
      >[/color]


      Comment

      • Steve

        #4
        Re: Tree structure

        Nevermind. I should have done some more searching first :-)

        If anyone is trying to do the same thing then they might want to check out
        this article:



        It has an excellent approach to it, starting from page 2.

        Steve


        "Steve" <me@removethisb it_stephenmcnab b.com> wrote in message
        news:402ff715$0 $7065$cc9e4d1f@ news-text.dial.pipex .com...[color=blue]
        >
        > I'm looking for some advice on how to design a set of tables to represent[/color]
        a[color=blue]
        > tree style table structure in MySQL.
        >
        > I have a table that stores a set of items, with each item having a type.[/color]
        The[color=blue]
        > type of the item belongs to a tree structure of types.
        >
        > e.g.
        >
        > Type1 +---- Type2 ---- Type3 +---- Type4
        > | +---- Type5
        > |
        > +---- Type6 ---- Type7
        >
        > Type8 +---- Type9
        > +---- Type10 +---- Type11
        > +---- Type12
        >
        > I need to run queries on the item table to pull back all items that belong
        > to a type or any of it's child types
        >
        > e.g.
        >
        > where type = Type2 or type = Type3 or type = Type4 or type = Type5
        > or
        > where type = Type10 or type = Type11 or type = Type12
        >
        > (I hope the diagram and psuedo code helps to explain what I'm trying to
        > achieve :-))
        >
        > At the moment I store the structure of the type tree in an array within[/color]
        PHP[color=blue]
        > and build up a long where clause but someone hinted that there might be a
        > more efficient database approach to this - a cross reference table?
        > Unfortunately I didn't get a chance to discuss it further with him.
        >
        > If anyone could suggest an approach or even point me in the general
        > direction of some good info on the concept I'd much appreciate it,
        >
        > Steve
        >
        >[/color]


        Comment

        Working...