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 6819493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:18:46+00:00 2026-05-26T21:18:46+00:00

I have 5 tables: user > user_has_role > role > role_has_permission > permission An

  • 0

I have 5 tables: user > user_has_role > role > role_has_permission > permission

An user can have 0 or * roles.
And a role can have 0 or * permissions

now i have a query which will get all permissions belonging to a user.
This work but now i want that if the user has the role “super_admin” then all permissions get joined. But without specifing them in the table role_has_permission.

This is query:

SELECT        p.name,r.name role
FROM          user_has_role AS ur
JOIN          role AS r
ON            ur.role_id = r.id
JOIN          role_has_permission AS rp
ON            r.id = rp.role_id
LEFT JOIN     permission AS p
ON            rp.permission_id = p.id
WHERE         ur.user_id = [USER_ID]
  • 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-26T21:18:47+00:00Added an answer on May 26, 2026 at 9:18 pm

    You could just do a union:

    SELECT        p.name,r.name role
    FROM          user_has_role AS ur
    JOIN          role AS r
    ON            ur.role_id = r.id
    JOIN          role_has_permission AS rp
    ON            r.id = rp.role_id
    LEFT JOIN     permission AS p
    ON            rp.permission_id = p.id
    WHERE         ur.user_id = [USER_ID]
    UNION
       SELECT p.name, 'super_admin' 
       FROM permission 
       WHERE EXISTS (SELECT * FROM user_has_role, role 
                  WHERE ur.user_id = [USER_ID] and role.role_id = user_has_role.role_id and             
                  role.name = 'super_admin')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table containing roles. Many users can be assigned to a role.
I have the following tables: USER ID VARCHAR(32) INTEGER NOT NULL ROLE ID INTEGER
I have two tables named USER and ROLE with entities as USER and ROLE
I have 3 simple tables: user, role, user_x_role with Many-To-Many relation. I have 2
Right now I have two tables, users and roles, of course roles has a
I have the following entities: public class User { public virtual ICollection<Role> Roles {
I have a a user that has a lot of tables against their account.
I have Messages and User models with corresponding tables. The Messages table has such
I have a table User which has the fields (id, first_name, middle_name, last_name). I
I have a User table which has a PrivilegeId foreign key points to a

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.