Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7054095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:35:05+00:00 2026-05-28T03:35:05+00:00

I have a user table from which I want all values, so I have

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:35:06+00:00Added an answer on May 28, 2026 at 3:35 am

    Update: added LEFT JOIN for users with no role.

    SELECT
      tbl_user.*,
      GROUP_CONCAT(role_name) AS roles
    FROM 
      tbl_user LEFT JOIN tbl_roles ON tbl_user.userid = tbl_roles.userid
      JOIN tbl_rolenames ON tbl_roles.roleid = tbl_rolenames.roleid
    GROUP BY tbl_user.userid
    

    Note that MySQL will permit a GROUP BY on fewer columns than appear in the SELECT list in total, but in other RDBMS you would need to explicitly list out the columns in tbl_user and include them in the GROUP BY, or do an additional self join against tbl_user to get the remaining columns from that table.

    Something like:

    SELECT 
      urole.userid,
      uall.username,
      uall.name,
      uall.othercols,
      urole.roles
    FROM
      tbl_user uall JOIN (
      SELECT 
        tbl_user.userid, 
        GROUP_CONCAT(role_name) AS roles
      FROM 
        tbl_user LEFT JOIN tbl_roles ON tbl_user.userid = tbl_roles.roleid
        JOIN tbl_rolenames ON tbl_roles.roleid = tbl_rolenames.roleid
      GROUP BY tbl_user.userid
    ) urole ON uall.userid = urole.userid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of data sorted by date, from which a user can
I have an user defined table function in SQL Server that aggregate data from
When designing user table what would be the must have fields from the security/user
Is it possible to have a relationship from a user table to a system
I have a User table that has all of their avatars saved in an
I have a User table which has a PrivilegeId foreign key points to a
Currently we have a table with a bunch of data from a database which
I want to allow the user to define a query which will be used
For example, I have a table which looks like this : id | name
I have a SELECT statement that looks like this: SELECT * FROM photos, p_votes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.