Trying to do this with a single query, but want to get a list of all users who have permission to do x. This involves the users table and permissions table. But it looks like any joins I use doesnt care about the where statement (since the user will usually have another permission in the table)
SELECT DISTINCT `u`.*
FROM (`system_users` AS u)
JOIN `system_user_permissions` AS p ON `u`.`id` = `p`.`user`
WHERE `p`.`permission` != 2
AND `p`.`permission` != 3
I have about 3900 users, I should receive about 2000 users who are not tied to either permission id 2 or 3. But still get 3900 The problem is because the user may also be tied to permission 1,5,6… they still get put in the list after it groups them together.
Any suggestions?
Updated query, still no luck though. If I output “permission” for each one I don’t get 2 or 3, the users listed still may have that permission there
SELECT DISTINCT `u`.*, `p`.`permission`
FROM (`system_users` AS u)
INNER JOIN `system_user_permissions` AS p ON `u`.`id` = `p`.`user`
WHERE `p`.`permission` NOT IN (2, 3)
This did the trick:
SELECT * FROM system_users AS u
WHERE NOT EXISTS(
SELECT 1 FROM system_user_permissions AS p
WHERE u.id = p.user
AND p.permission IN (2,3)
)
You want
not exists:All your original query is doing is negating any user that has only permission 2 or 3, not 1, 2, and 5 (which is what you want). A
not existswill throw that user out as soon as a permission 2 or 3 is found insystem_user_permissionsfor that user.