First I'd like to point out that as I understand your structure it is not a real tree, because (unless you forgot to mention this restriction) two different groups can belong to a same parent group.
I had some thoughts on similar problems in an access-control setting, so let me use this language and call an item "user" and a group "group" (not much of a change, is it?).
A user can belong to one or more groups and each group can belong to (and have the access rights of) one or more groups. Say antonio is in group <students> and in group <football players>. Both <students> and <football players> belong to <people>, and <people> belongs to the <universal> group. If the problem is getting all the groups antonio belongs to, you could first look for all the groups it directly belongs to and then climb on through the hierarchy. Thus, you get
antonio -> <students>, <football players>
at the next step
antonio -> <students>, <football players>, <people>
(you only consider <people> once), and finally
antonio -> <students>, <football players>, <people>, <universal>

Steps can proliferate if you have deep levels of nesting.
To improve performance during the lookup, I would create the db this way:
users:
------------------------------
| ID | name      | fields... |
------------------------------
|  1 | antonio   |           |
     ...
|________________|___________|

groups:
-------------------------------------
| ID | name             | fields... |
-------------------------------------
|  1 | students         |           |
|  2 | football players |           |
|  3 | people           |           |
|  4 | universal        |           |
     ...
|____|__________________|___________|

user_group_rel (user belongs to group)
----------------------
| ID_user | ID_group |
----------------------
| 1       | 1        |
| 1       | 2        |
    ...
|_________|__________|

group_group_rel (group inherits from group)
-------------------------------
| ID1  | ID2 ( parent group ) |
-------------------------------
| 1    | 1                    |
| 1    | 3                    |
| 1    | 4                    |
| 2    | 2                    |
| 2    | 3                    |
| 2    | 4                    |
| 3    | 3                    |
| 3    | 4                    |
      ...
|______|______________________|
Note that the group-group relationship contains all rows (n,n) and all possible group inclusions, including derived ones (e.g. <students>-><universal>).
This requires more rows to be generated, but then you can retrieve all groups a user belongs to by the one query
SELECT UNIQUE group_group_rel.ID2
  FROM group_group_rel, user_group_rel
  WHERE user_group_rel.ID_user=?
    AND user_group_rel.ID_group=group_group_rel.ID1
If you want to keep track of what group inclusions are given and what are derived, you could add a boolean column in the group_group_rel table.

Best regards

Antonio Bellezza

Update: Added explanatory remark in italics.

In reply to Re: storing tree-like structures in tables by abell
in thread storing tree-like structures in tables by schweini

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.