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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:07:42+00:00 2026-06-07T16:07:42+00:00

I have two tables users with columns: uid, name, mail, etc users_roles with columns:

  • 0

I have two tables
users with columns: uid, name, mail, etc
users_roles with columns: uid, rid

In users_roles, each time a role is added to a user, it is listed in the users_roles table. So, let’s say user 1 has roles 1 and 4. In the table:

users_roles:  
uid | rid  
 1  |  1  
 1  |  4  

I need to return all users who don’t have roles 4 OR 5. I have tried using both Group By and Distinct combined with NOT IN. The problem I keep running into is if a user has both roles 1 and 4, they will be returned in the results. Below is a an example of my Group By query:

SELECT *
FROM users AS u
LEFT JOIN users_roles AS ur ON u.uid = ur.uid
WHERE ur.rid NOT
IN ( 4, 5 )
GROUP BY ur.uid

I have tried sub-queries as well to no avail because the issue seems to be that Group By combines rows after finishing the query. So, it simply finds the record containing uid 1 rid 4 and returns it in the results.

The Drupal module Views that I can’t use (due to security issues with Views Bulk Operations) accomplishes the desired results by doing the following:

LEFT JOIN users_roles ON users.uid = users_roles.uid 
AND (users_roles.rid = '4' OR users_roles.rid = '5')

For long term maintenance I don’t want to have to update the code every single time we add a role and this is going to make for one long query.

I looked at the following:
Aggregating Rows
Filtering distinct rows in SQL

While there are Drupal functions that will let me get the list of role ids where I could unset the roles I don’t want show up in the resulting array, I feel like I am missing a fundamental understanding of SQL. Is there a better way to do this in SQL?

  • 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-07T16:07:44+00:00Added an answer on June 7, 2026 at 4:07 pm

    I need to return all users who don’t have roles 4 & 5

    select  *
    from    users u
    where   not exists
            (
            select  *
            from    users_roles ur
            where   ur.rid in (4,5)
                    and ur.uid = u.uid
            )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have two tables: Users and News. Table News has columns id, name, createdby,
I have two separate tables both with user id columns uid . I want
I have two tables: Recipes RecipeId | UserId | Name | etc. and RecipeRatings
I have two tables: Personnel with ID,Name,... columns and User with PersonnelID (FK of
I have two Tables: Users and Permission. They share 2 columns with the same
I have two tables with these columns: Users: user, class, grade, location. Classes: class,
I have two tables: events and events_actions events columns: event_id user_id event_name events_actions columns:
i have four tables user-question contains two columns: questionID, userID, the questions that the
I have two tables: Users: ID, first_name, last_name Networks: user_id, friend_id, status I want
I have two tables- users and language with a foreign key link of their

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.