简体   繁体   English

Mysql加入,检查用户是否拥有/没有权限

[英]Mysql Join, check if user has / has no permission

Trying to do this with a single query, but want to get a list of all users who have permission to do x. 尝试使用单个查询执行此操作,但希望获得有权执行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) 但看起来我使用的任何连接都不关心where语句(因为用户通常会在表中有另一个权限)

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. 我有大约3900个用户,我应该收到大约2000个与权限ID 2或3都不相关的用户。但是仍然获得3900问题是因为该用户也可能与权限1,5,6相关...在将它们组合在一起后放入列表中。

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 如果我为每个输出“许可”,我没有得到2或3,列出的用户仍然可以在那里获得该许可

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 : not exists希望not exists

select
   *
from
   system_users u
where
   not exists (
       select 
           1 
       from 
           system_user_permissions p 
       where 
           p.id = u.user 
           and p.permission in (2,3)
   )

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). 您的所有原始查询所做的是否定任何只有权限2或3,而不是1,2和5(这是您想要的)的用户。 A not exists will throw that user out as soon as a permission 2 or 3 is found in system_user_permissions for that user. 一旦在该用户的system_user_permissions找到权限2或3, not exists将抛出该用户。

You could approach the opposite way, specifiying which users shouldn't appear in the list, like this: 您可以采用相反的方式,指定哪些用户不应出现在列表中,如下所示:

SELECT DISTINCT `u`.*
FROM (`system_users` AS u)
JOIN `system_user_permissions` AS p ON `u`.`id` = `p`.`user`
WHERE `p`.`user` NOT IN
  (SELECT DISTINCT `user`
   FROM `system_user_permissions`
   WHERE `permission` = 2 OR `permission` = 3);

Use the NOT IN keywords to select a group of values you do not want returned. 使用NOT IN关键字选择一组您不想返回的值。 Obviously, removing the "NOT" would have the opposite affect. 显然,删除“NOT”会产生相反的影响。

SELECT DISTINCT `u`.* 
FROM (`system_users` AS u) 
INNER JOIN `system_user_permissions` AS p 
   ON `u`.`id` = `p`.`user` 
WHERE `p`.`permission` not in (2,3)

Have you tried this? 你试过这个吗? I think your problems is () 我认为你的问题是()

SELECT DISTINCT u .* FROM ( system_users AS u) JOIN system_user_permissions AS p ON u . SELECT DISTINCT u 。* FROM( system_users AS u)JOIN system_user_permissions AS p ON u id = p . id = p user WHERE ( p . permission != 2 AND p . permission != 3 ) userppermission != 2和ppermission != 3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM