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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:15:56+00:00 2026-06-19T02:15:56+00:00

I have an SQL query that selects user’s privileges, and adds true to them.

  • 0

I have an SQL query that selects user’s privileges, and adds true to them.

SELECT
    PrivilageName,
    'true' hasrights  <-- imaginary column
FROM
    users
NATURAL JOIN usermemberships
NATURAL JOIN groupprivileges
NATURAL JOIN `privileges`
WHERE
    UserID = '2'

Result is

AddBuilding           true
RemoveBuilding        true
EditBuilding          true

I’m trying to add the rest of the privilages with false value.

AddBuilding           true
RemoveBuilding        true
EditBuilding          true
RemoveUser            false
AddUser               false

How I’ll do this?

Edit: the structure of the tables:

users(UserID),
usermemberships(UserID, groupID),
groupprivileges(GroupID, PrivilegeID),
privileges(PrivilegeID, PrivilageName)

Edit: misspelling, sorry.

  • 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-19T02:15:58+00:00Added an answer on June 19, 2026 at 2:15 am

    (NOTE: The queries in this answer are now updated, to include the column names that were added to the question.)

    One approach to getting that resultset would be to use LEFT JOIN operations (with appropriate predicates in the ON caluse), in place of all those NATURAL JOIN operations.

    (I’m just guessing at the column names referenced by the NATURAL JOIN. In order to decipher that, we would need to inspect each table definition to get a list of all of the columns, and then find all the column names that match, to figure out which columns MySQL is using to do those inner join operations.)

    Based on the scant information provided in the query text, here’s the approach I would take (again, just guessing at the names referenced in each ON clause):

    SELECT p.PrivilageName
         , IF(u.UserID IS NOT NULL,'true','false') AS hasrights
      FROM `privileges` p
      LEFT
      JOIN groupprivileges g 
        ON g.PrivilegeID = p.PrivilegeID
      LEFT
      JOIN usermemberships m
        ON m.GroupId = g.GroupID
      LEFT
      JOIN users u
        ON u.UserID = g.UserID
       AND u.UserID = 2
    

    Depending on the cardinality in those tables (i.e. is “AddBuilding” privilege granted to two different groups, one which the user is a member of and the other not…)

    and depending on whether you want to avoid returning any “duplicate” PrivilageName values (either multiple rows with “true” or “false”, or rows with both “true” and “false” for each PrivilageName), and depending on how you want the resultset ordered (i.e. do you want all the “true” privileges listed first?)…

    Then this query is more deterministic in the resultset that is returned, it will return each PrivilageName only once. This resultset seems better suited to answer the question whether a user has a privilege or not.

    SELECT p.PrivilageName
         , MAX(IF(u.UserID IS NOT NULL,'true','false')) AS hasrights
      FROM `privileges` p
      LEFT
      JOIN groupprivileges g 
        ON g.PrivilegeID = p.PrivilegeID
      LEFT
      JOIN usermemberships m
        ON m.GroupId = g.GroupID
      LEFT
      JOIN users u
        ON u.UserID = g.UserID
       AND u.UserID = 2
     GROUP BY p.PrivilageName
     ORDER BY hasrights DESC, p.PrivilageName ASC
    

    (Personally, I’d omit the ORDER BY, and let the results be ordered by PrivilageName, but with the ORDER BY, this better matches the resultset specified in the question.)


    Of course, that’s not the only way to get the result set, but it’s likely to be the most efficient (given suitable indexes).

    Personally, I don’t ever use NATURAL JOIN. (I want to see the predicates in the statement, and I don’t want any of my queries to “break” if someone adds a column with a matching name to one of the table in my query. (Actually, thinking about it, I can’t use NATURAL JOIN because id is typically the name of the primary key column of nearly all my tables… foreign key columns are typically named referencedtable_id.) But even if I did name the columns in a way that I could use NATURAL JOIN, I see the potential drawbacks outweighing any advantage.

    But, something like the statement below might work. (I say “might” because I don’t have any experience using syntax like this… I never use NATURAL JOIN, and I always prefer LEFT joins to RIGHT joins. If someone in my shop came to me with this, I would give them the statement above. But I don’t want to leave you with the impression that a NATURAL JOIN can’t be used to return the specified resultset. It’s possible your specified resultset might be returned by a statement like this:

    SELECT
        PrivilageName,
        MAX(IF(UserID=2,'false','true')) AS hasrights
    FROM
        users
    NATURAL RIGHT JOIN usermemberships
    NATURAL RIGHT JOIN groupprivileges
    NATURAL RIGHT JOIN `privileges`
    GROUP BY PrivilageName
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL query that returns a Datatable: var routesTable = _dbhelper.Select(SELECT [RouteId],[UserId],[SourceName],[CreationTime]
I have a SQL query that goes like this: UPDATE User SET flag='Y' WHERE
I have this SQL query : INSERT INTO db1.outbox (DestinationNumber, TextDecoded) SELECT User.CellPhone, '$SMSMessage'
I have a sql query that I return into a DataTable: SELECT TABLE_NAME, COLUMN_NAME,
I have this SQL Query that pulls data from 3 tables. I am unable
I have a SQL query that I'm trying to debug. It works fine for
I have an SQL query that returns all companies records ignore the ones with
I have a SQL query that I'm trying to write, but I'm not quite
Suppose I have a SQL Query that isn't really nice. The SQL Query has
I currently have a SQL query that returns a number of fields. I need

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.