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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:52:30+00:00 2026-06-14T08:52:30+00:00

I have the following database tables : user int unsigned id varchar login group

  • 0

I have the following database tables :

user
    int unsigned id
    varchar      login

group
    int unsigned id
    varchar      label

right
    int unsigned id
    varchar      label

user_group
    int unsigned userId
    int unsigned groupId

user_right
    int unsigned userId
    int unsigned rightId

group_right
    int unsigned groupId
    int unsigned rightId

A right is considered as granted to a group or a user when it’s associated using user_right or group_right tables (respectivly).
I have the following query that is supposed to retrieve an user’s rights from his login :

SELECT DISTINCT `R`.`id`,
                `R`.`label`
FROM `user` `U`,
     `right` `R`,
     `user_right` `UR`,
     `user_group` `UG`,
     `group_right` `GR`
WHERE (`U`.`id` = `UR`.`userId`
       AND `R`.`id` = `UR`.`rightId`
       OR `U`.`id` = `UG`.`userId`
       AND `UG`.`groupId` = `GR`.`groupId`
       AND `R`.`id` = `GR`.`rightId`)
  AND `U`.`login` = 'admin'

When a user is associated to rights and groups, the query does work and returns the right rights. But when a user has no group, the query does not work. If i remove the unused tables (user_group and group_rights), it does work again.

What should i add to my query to avoid this ? Should I add something like “OR some_column IS NULL” ? I’d appreciate if you could also explain me why so i don’t meet this problem again.

Thanks in advance.

Some sample datas :

user (id, login)
1 'admin'

right (id, label)
1 'r1',
2 'r2',
3 'r3'

user_right (id, label)
1 1

Thanks for reading 🙂

  • 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-14T08:52:32+00:00Added an answer on June 14, 2026 at 8:52 am

    First of all, you are using implicit joins, and, the way you wrote your query, they are INNER JOINs. You should change your query to use proper explicit joins, and use LEFT JOINs in there. Something like this:

    SELECT DISTINCT `R`.`id`,
                    `R`.`label`
    FROM `user` `U`
    LEFT JOIN `user_right` `UR`
        ON `U`.`id` = `UR`.`userId`
    LEFT JOIN `right` `R`
        ON `R`.`id` = `UR`.`rightId`
    LEFT JOIN `user_group` `UG`
        ON `U`.`id` = `UG`.`userId`
    LEFT JOIN `group_right` `GR`
        ON `UG`.`groupId` = `GR`.`groupId` AND `R`.`id` = `GR`.`rightId`
    WHERE `U`.`login` = 'admin'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following (simplified) database tables: Products - ProductID, int, PK - Name,
I have two database tables with the following structure: actions: action_id int(11) primary key
I have the following database structure (which can't be changed): Users - UserID (int)
I have three mysql table from same database Db1. Three tables have following columns.
I have the following tables in my database: Products ID_PRODUCT PRODUCTNAME PRICE Customers ID_CUSTOMER
I have the following tables in a database (i'll only list the important attributes):
I have the following two tables in my mysql database. Table Name: groups id
I have the following tables, with these keys in my database: bookings session_id sessions
I have a database with two tables, tblPlayers and tblMatches. tblPlayers contains the following
I have a database with the following tables, Member, Paper, Topic, Interest, Associated. I

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.