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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:48:14+00:00 2026-05-21T07:48:14+00:00

Basically, I have two tables, admin_privilege and admin_roll_privilege. I’m trying to write a query

  • 0

Basically, I have two tables, admin_privilege and admin_roll_privilege. I’m trying to write a query to get every row from admin_privilege, and if there is a row in admin_roll_privilege with a matching admin_privilege_id AND a matching admin_roll_id, to set a new column to 1. So far, I have this:

  SELECT ap.*, 
    IF(arp.admin_privilege_id IS NULL,0,1) AS has_privilege
  FROM admin_privilege ap LEFT JOIN admin_roll_privilege arp
    ON ap.admin_privilege_id=arp.admin_privilege_id
  WHERE arp.admin_roll_id=3 
    OR arp.admin_roll_id IS NULL;

This works in every case except where there are no matching rows admin_roll_privilege.

See Example:

+---------------+--------------------+
| admin_roll_id | admin_privilege_id |
+---------------+--------------------+
|             1 |                  2 |
|             1 |                  3 |
+---------------+--------------------+

+--------------------+------------------------+
| admin_privilege_id | admin_privilege_name   |
+--------------------+------------------------+
|                  1 | Access Developer Tools |
|                  4 | Edit System Settings   |
|                  2 | Edit User Profiles     |
|                  3 | Resolve Challenges     |
+--------------------+------------------------+

Querying for WHERE admin roll id=1 works as expected:

+--------------------+------------------------+---------------+
| admin_privilege_id | admin_privilege_name   | has_privilege |
+--------------------+------------------------+---------------+
|                  1 | Access Developer Tools |             0 |
|                  4 | Edit System Settings   |             0 |
|                  2 | Edit User Profiles     |             1 |
|                  3 | Resolve Challenges     |             1 |
+--------------------+------------------------+---------------+

But, if i query for admin_roll_id=3, i only get two rows returned:

+--------------------+------------------------+---------------+
| admin_privilege_id | admin_privilege_name   | has_privilege |
+--------------------+------------------------+---------------+
|                  1 | Access Developer Tools |             0 |
|                  4 | Edit System Settings   |             0 |
+--------------------+------------------------+---------------+

How can I get this query to return all 4?

Edit: This is what ended up working, moving the condition to the on clause:

SELECT ap.*, 
    IF(arp.admin_privilege_id IS NULL,0,1) AS has_privilege
  FROM admin_privilege ap LEFT JOIN admin_roll_privilege arp
    ON (ap.admin_privilege_id=arp.admin_privilege_id AND arp.admin_roll_id=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-05-21T07:48:14+00:00Added an answer on May 21, 2026 at 7:48 am

    Move the appropriate conditions from the WHERE clause to the ON clause.

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

Sidebar

Related Questions

I basically created some tables to play around with: I have Two main tables,
I basically have two tables: A(id int, name varchar(10), info varchar(10)) B(id int, item
I basically have two tables: A(id int, name varchar(10), info varchar(10)) B(id int, item
Basically you have two ways for doing this: for (int x = 0; x
I have basically two separate sites, a SharePoint collaboration site, and an ASP.Net application
I have a one to many relationship between two tables. The many table contains
I basically have a page which shows a processing screen which has been flushed
I basically have the following flow: XML -> JSON -> Spring MVC -> jsp
I am looking in to ways to enable a site to basically have something
I would like to do the following. Basically have a stored procedure call another

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.