How would I setup a groups table with following relationship with a list of user:
For example, lets say I have table of users:
user_list
-----------------------------------
first_name | Last Name | user_id |
-----------------------------------
john | Appleseed | 4 |
Jasmin | Applejuice | 6 |
Now I want to create a groups table. How can I create a table where users can be included in a group, or multiple groups. For example, John is included in the Apple_group
but also included in the Orage_group
. Jasmin is only included in the Orange_group
:
groups
----------------------------------------------------
group_name | included_users_ids | group_id |
----------------------------------------------------
Apple_group | 4 | 1 |
Orange_group | 4, 6 | 6 |
Assuming the user_list table already exists, how would I setup the groups table? Furthermore I've read (see link below) that I shouldn't include multiple values in a single column.
Inserting a foreign key with multiple values
So, my questions are:
What would be the appropriate way to setup the groups table?
What would the SQL be for doing so?
How would this then be handled with PHP when wanting to set a user to a group?