So I have the following table, users:
+-----------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+-------+
| uid | int(10) unsigned | NO | PRI | 0 | |
| name | varchar(60) | NO | UNI | | |
---------------------------------------------------------------
And the table, roles:
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| rid | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(64) | NO | UNI | | |
+-------+------------------+------+-----+---------+----------------+
I have a third table, users_roles, which in a one-to-many relationship maps users to one or more roles:
+-------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| uid | int(10) unsigned | NO | PRI | 0 | |
| rid | int(10) unsigned | NO | PRI | 0 | |
+-------+------------------+------+-----+---------+-------+
Finally, I have a fourth table, memberships, that maps one-to-one with users:
+-------------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------------+------+-----+---------+-------+
| membership_no | varchar(32) | NO | PRI | NULL | |
| uid | int(11) | YES | | NULL | |
--------------------------------------------------------------------
Currently I have a nice and simple query that provides a list of users with their membership number:
SELECT u.uid, u.name, m.membership_no FROM users AS u
LEFT JOIN memberships AS m ON u.uid = m.uid;
But now I would like to add an additional column to my query that looks up all the users_roles entries once per user, and then concatenates each roles.name in one column, so I can see all the roles for that user. An example output might be:
uid name membership no roles
--- ---- ----- ------
1 foo 123432 admin, normal user, student
2 bar baz 235235 admin
3 bak 2352352 normal user, student
So my task is how I would integrate this additional query on the users_roles table, link user_roles.rid to roles.rid and concatenate the data in a column. Any pointers?