In my application I have a table "user", a table "role", and a table "userroles". Userroles is simple table consisting of user, role
, where user
corresponds to the id
column of the user
table. As user can have multiple roles, and obviously multiple users can have the same role. Now I wish to find all users who do NOT have the specific role (private_account
), and I wish to list them as (user_id, new-inserted-role-id)
for further processing.
SELECT u.id, r.id
FROM public."user" AS u
WHERE u.id NOT IN (
SELECT ur.user
FROM public."userroles" as ur
INNER JOIN public."role" as r
ON ur.role=r.id
WHERE r."roleDescription"='private_account'
)
However this complains that the table r
does not exist.
ERROR: missing FROM-clause entry for table "ur"
How can I do this?
So for clarity: if private_account
would have id 2
and I a user 1 which does not have this ID the return would look like:
USER | role
1 | 2
2 | 2
The role reported is independent on what the user actually has. I could do this manually and do a select like below. However this has the huge disadvantage that I have to manually update the sql if I move to a different database (since the ID might not correspond to the actual role name).
SELECT u.id, 2
...
The further processing is basically using the return from the select as INSERT INTO
:
INSERT INTO public."userroles"
SELECT u.id, r.id
FROM public."user" AS u
WHERE u.id NOT IN (
SELECT ur.user
FROM public."userroles" as ur
INNER JOIN public."role" as r
ON ur.role=r.id
WHERE r."roleDescription"='private_account'
)
Best Answer
Create tables:
Populate test data
Find all users and their roles:
Find all users who do NOT have the specific role:
This last query is your answer.