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

  • SEARCH
  • Home
  • 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 9205997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:05:00+00:00 2026-06-18T00:05:00+00:00

the three tables I am trying to query are: DB Structure: roles_permissions: id, role_id,

  • 0

the three tables I am trying to query are:

DB Structure:

roles_permissions: id, role_id, permission_id

user_roles: id, name, user_id

permissions: id, name

SQL Query

SELECT ur.name AS role_name,perm.name AS perm_name FROM user_roles AS ur
    INNER JOIN roles_permissions as p
    ON ur.id=p.role_id
    INNER JOIN permissions AS perm
    ON p.permission_id = perm.id
    WHERE ur.user_id='$account_id'

Result from above query

    array(11) {
  [0]=>
  array(2) {
    ["role_name"]=>
    string(5) "Owner"
    ["perm_name"]=>
    string(12) "view project"
  }
  [1]=>
  array(2) {
    ["role_name"]=>
    string(5) "Admin"
    ["perm_name"]=>
    string(12) "edit project"
  }
  [2]=>
  array(2) {
    ["role_name"]=>
    string(6) "Editor"
    ["perm_name"]=>
    string(14) "create project"
  }
  [3]=>
  array(2) {
    ["role_name"]=>
    string(5) "Owner"
    ["perm_name"]=>
    string(12) "view project"
  }
  [4]=>
  array(2) {
    ["role_name"]=>
    string(5) "Admin"
    ["perm_name"]=>
    string(12) "edit project"
  }
  [5]=>
  array(2) {
    ["role_name"]=>
    string(6) "Editor"
    ["perm_name"]=>
    string(14) "create project"
  }
  [6]=>
  array(2) {
    ["role_name"]=>
    string(5) "Owner"
    ["perm_name"]=>
    string(12) "view project"
  }
  [7]=>
  array(2) {
    ["role_name"]=>
    string(6) "Editor"
    ["perm_name"]=>
    string(12) "edit project"
  }
  [8]=>
  array(2) {
    ["role_name"]=>
    string(5) "Owner"
    ["perm_name"]=>
    string(12) "view project"
  }
  [9]=>
  array(2) {
    ["role_name"]=>
    string(5) "Admin"
    ["perm_name"]=>
    string(12) "edit project"
  }
  [10]=>
  array(2) {
    ["role_name"]=>
    string(6) "Editor"
    ["perm_name"]=>
    string(14) "create project"
  }
}

What I am trying to do is select all user_roles assigned to a specific user_id, then get the permission ids assigned to that role from roles_permissions, then get the names of each permission from the permissions table. I would like to group by user_roles.name

so the resulting array would be

    array(3) {
  ["Owner"]=>
  array(3) {
    ["perm_name"]=>
    string(12) "view project"
    ["perm_name"]=>
    string(12) "create project"
    ["perm_name"]=>
    string(12) "edit project"
  }
  ["Editor"]=>
  array(2) {
    ["perm_name"]=>
    string(12) "view project"
    ["perm_name"]=>
    string(12) "edit project"
  }
   ["Admin"]=>
  array(3) {
    ["perm_name"]=>
    string(12) "view project"
    ["perm_name"]=>
    string(12) "create project"
    ["perm_name"]=>
    string(12) "edit project"
  }

}

So if anyone can help that would be great, also looking to learn from this so if you could explain it for me that would be awesome.

Thanks

SQL FIDDLE : http://sqlfiddle.com/#!2/a7954/1

  • 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-06-18T00:05:01+00:00Added an answer on June 18, 2026 at 12:05 am

    This won’t directly solve your problem, but your schema has a number of issues. Chief among them is that the tuple (role_id, permission_id) in roles_permissions isn’t unique. Here’s how I’d restructure things:

    role
    ==========
    id  -- autoincrement
    name  -- varchar
    
    user_role
    ===========
    role_id  -- fk to role.id
    user_id  -- fk to user.id (not listed here, but you obviously have one)
    
    permission
    ===========
    id  -- autoincrement
    name  -- varchar
    
    role_permission
    ===================
    role_id  -- fk to role.id
    permission_id  -- fk to permission.id
    

    (note that if you’re attempting to do permission groups or role hierarchies this gets more complicated)

    The modified example fiddle showing the setup, with sample data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create three tables however I am getting error as You
I am using three tables and I am trying to calculate discounted price by
I have the following three tables. I am trying to inter link (join) among
I'm trying to create a query with dynamic columns, based on data from three
So I have three tables I am trying to pull data from with the
While implementing a tree structure over a SQL 2005 server database, the query response
I have two tables from which I'm trying to run a query to return
Lets say that I have a database structure with three tables that look like
I am trying to build a select query that will essentially left join two
I was trying to extract some info from the database using this query, SELECT

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.