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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:07:23+00:00 2026-05-28T20:07:23+00:00

I have five tables: User user_id | name ——————– 0 | Mark 1 |

  • 0

I have five tables:

User

 user_id | name
--------------------
    0    | Mark  
    1    | Jen
    2    | Mbali
    3    | Mbabani
    4    | Fang Zhao

Role

 role_id | name
--------------------
    3    | Employee
    4    | Customer Asia
    5    | Customer Africa

User_Role_Assoc

 role_id | user_id
--------------------
    3    |   0
    3    |   1
    3    |   2
    5    |   3
    4    |   4

Role_Reps

 role_id | user_id
--------------------
    4    |   0
    4    |   1

Request

 req_id  | user_id
--------------------
    8    |   3
    9    |   3
   10    |   4
   11    |   4

Mark, Jen and Mbali are all employees (role_id=3) of a fictional company. The other two users Mbabani and Fang Zhao are customers who create requests.

The query should be able to see that req_id 8 and 9 were requested by a user (Mbabani[3]) who belongs to the Customer Africa role (by way of User_Role_Assoc) which has no assigned representatives (Role_Reps).

The query should be able to see that req_id 10 and 11 were requested by a user(Fang Zhao[4]) who belongs to the Customer Asia role (by way of User_Role_Assoc) which DOES have representatives.

All employees should be able to see all requests. Unless there is a role representative assingned to that role in the Role_Reps table. If there are any representatives, in this case Mark and Jen, they are the ONLY ONES alowed to see the request. If there are NO representatives defined in the Role_Reps table then everyone should be able to see the requests.

So I need a query that:

If I pass in a userid=2 (Mbali) I should get the following results:

 req_id  | user_id
--------------------
   10    |   4
   11    |   4

If I pass in a userid=0 or userid=1 (Mark or Jen) I should get the following results:

 req_id  | user_id
--------------------
    8    |   3
    9    |   3
   10    |   4
   11    |   4

I hope I’ve made myself clear.


UPDATE

Here are the DDLs to generate the tables with the data:

DROP TABLE IF EXISTS t_user;


CREATE TABLE t_user (
    userid          integer PRIMARY KEY,
    name            varchar(20)
);

GRANT ALL PRIVILEGES ON t_user TO PUBLIC;

INSERT INTO t_user (userid, name) VALUES (0,'Mark');
INSERT INTO t_user (userid, name) VALUES (1,'Jen');
INSERT INTO t_user (userid, name) VALUES (2,'Mbali');
INSERT INTO t_user (userid, name) VALUES (3,'Mbabani');
INSERT INTO t_user (userid, name) VALUES (4,'Fang Zhao');



DROP TABLE IF EXISTS t_role;


CREATE TABLE t_role (
    roleid          integer PRIMARY KEY,
    name            varchar(20)
);

GRANT ALL PRIVILEGES ON t_role TO PUBLIC;

INSERT INTO t_role (roleid, name) VALUES (3,'Employee');
INSERT INTO t_role (roleid, name) VALUES (4,'Customer Asia');
INSERT INTO t_role (roleid, name) VALUES (5,'Customer Africa');


DROP TABLE IF EXISTS t_user_role_assoc;


CREATE TABLE t_user_role_assoc (
    roleid          integer,
    userid          integer,
    primary key(roleid, userid)
);

GRANT ALL PRIVILEGES ON t_user_role_assoc TO PUBLIC;

INSERT INTO t_user_role_assoc (roleid, userid) VALUES (3,0);
INSERT INTO t_user_role_assoc (roleid, userid) VALUES (3,1);
INSERT INTO t_user_role_assoc (roleid, userid) VALUES (3,2);
INSERT INTO t_user_role_assoc (roleid, userid) VALUES (5,3);
INSERT INTO t_user_role_assoc (roleid, userid) VALUES (4,4);




DROP TABLE IF EXISTS t_role_reps;


CREATE TABLE t_role_reps (
    roleid          integer,
    userid          integer,
    primary key(roleid, userid)
);

GRANT ALL PRIVILEGES ON t_role_reps TO PUBLIC;

INSERT INTO t_role_reps (roleid, userid) VALUES (4,0);
INSERT INTO t_role_reps (roleid, userid) VALUES (4,1);


DROP TABLE IF EXISTS t_request;


CREATE TABLE t_request (
    req_id          integer PRIMARY KEY,
    userid          integer
);

GRANT ALL PRIVILEGES ON t_request TO PUBLIC;

INSERT INTO t_request (req_id, userid) VALUES (8,3);
INSERT INTO t_request (req_id, userid) VALUES (9,3);
INSERT INTO t_request (req_id, userid) VALUES (10,4);
INSERT INTO t_request (req_id, userid) VALUES (11,4);
  • 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-28T20:07:23+00:00Added an answer on May 28, 2026 at 8:07 pm

    Just as general advice, I find it’s often easier to see what’s going on by making a new set of relations using natural keys instead of surrogate ones. So in this case, I might do this:

    User
    -------------------------
     username    | name
    -------------------------
      mark       | Mark  
      jen        | Jen
      mbali      | Mbali
      mbabani    | Mbabani
      fangzhao   | Fang Zhao
    
    Role
    ------------------------------------
     role_id          | name
    ------------------------------------
      employee        | Employee
      customer_asia   | Customer Asia
      customer_africa | Customer Africa
    
    User_Role_Assoc
    ------------------------------
     role_id          | user_id
    ------------------------------
      employee        | mark
      employee        | jen
      employee        | mbali
      customer_africa | mbabani
      customer_asia   | fangzhao
    
    Role_Reps
    ----------------------------
     role_id          | user_id
    ----------------------------
      customer_asia   | mark
      customer_africa | jen
    
    Request
    ---------------------
     req_id  | user_id
    ---------------------
      8      | mbabani
      9      | mbabani
     10      | fangzhao
     11      | fangzhao
    

    Now when you go to construct your query, you don’t have to do all the joins at once, you can interactively add another join and another and it will be much clearer to you as you get closer to what you need. When you’re done you can take that query right back to your actual database with surrogate IDs for everything, and it will “just work.”

    With respect to your actual question, you’re counting on the absence of something triggering the presence of something else. I usually find this kind of thing to be difficult to express in SQL. You may find it easier if you add additional data to one of the relations to signify that something is public or global, and do the restricted query UNIONed with a separate query to get all global stuff.

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

Sidebar

Related Questions

I have five tables: tab_template template_group group user_group user Tab_template's are organized into groups
I have five tables: models : id, name, specification models_networks : id, model_id, network_id
I have five mysql tables. shops +----+--------------+--------------+ | id | name | address |
I have five tables in my database. Members, items, comments, votes and countries. I
I have the following five tables: ISP Product Connection AddOn AddOn/Product (pivot table for
I have a database with five tables in an Android application. I have been
I have five models and their respective tables are Member:id,fname,lname Student:id,member_id(foreign key references member
I have five tables and i want to get result out of them. Here
I'm trying to get this to work... I have a five tables that Im
I have five tables called Asset Master , Category Master , Asset Category ,

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.