I have a user table from which I want all values, so I have this query:
SELECT tbl_user.* FROM tbl_user
Now I want one additional column in this result which shows all roles this user has, (or nothing if there are no roles for the user). The role information comes from two additional tables.
The first table contains these two values: userid, roleid
The second table contains roleid and role_name.
So the group concat needs to get all role names based on the roleid’s in table1.
I have tried several different ways to do this, but I don’t succeed. Either I get only one result with several times the same rolename, or no result at all.
Thanks for your help
Michael
Update: added
LEFT JOINfor users with no role.Note that MySQL will permit a
GROUP BYon fewer columns than appear in theSELECTlist in total, but in other RDBMS you would need to explicitly list out the columns intbl_userand include them in theGROUP BY, or do an additional self join againsttbl_userto get the remaining columns from that table.Something like: