Hi guys,
Thanks to the help from everyone in my other thread (and Derokorians example), i've got 3 tables...
There is a user table - and a group table. The 3rd lets groups and users be put into other groups. Allowing groups-in-groups, and users in groups.
USER
#userID
userName
userID | userName
---------------------
1 | Al
---------------------
2 | Babs
---------------------
3 | Chris
---------------------
4 | Don
GROUP
#groupID
groupName
groupID | groupName
---------------------
1 | Group-A
---------------------
2 | Group-B
---------------------
3 | Group-C
---------------------
4 | Group-D
GROUP-MEMBER
#groupMemberID
memberType - enum('group','user')
memberID
groupMemberID | memberType | memberID
---------------------------------------------
1 (Group-A) | user | 1 (Al)
---------------------------------------------
1 (Group-A) | user | 2 (Babs)
---------------------------------------------
1 (Group-A) | group | 2 (Group-B)
---------------------------------------------
2 (Group-B) | user | 1 (Al)
---------------------------------------------
2 (Group-B) | group | 4 (Group-D)
I would like to query the tables -- to get the group name, the number of groups in-the-groups, and the number of users in the group
| Single Users | Groups
--------------------------------------
Group-A | 2 | 1
--------------------------------------
Group-B | 1 | 1
--------------------------------------
Group-C | 0 | 0
--------------------------------------
Group-D | 0 | 0
I have no idea how to do this 🙁 -- But i am going to search the forum -- and see if i can work it out! I'm sure someone has asked something like that before 🙂
The real question i have is -- is there any way to count the number of users in the groups - "within" the groups.
So in the example - "GROUP-A" has 2 users - and also contains "GROUP B" - group-b holds 1 user, and "GROUP-D" -- group-4 has nothing in it -- so the total users in GROUP-A is 3 users (in all child groups, and in group-a itself). Would you do this in php with a loop? Or is there a way to do this in mysql?
I hope this makes sense. Sorry for the long thread! 🙂
(I'll make sure before people add groups-in-groups that parent groups cannot be put into child groups -- to stop any infinite loops)
Thanks again!