gpt4 book ai didi

MySQL:从组描述中重新查找用户组

转载 作者:行者123 更新时间:2023-11-29 16:33:27 26 4
gpt4 key购买 nike


我在查找用户的组成员身份时遇到问题。 (是的,这不是很清楚)
例如:
我有 2 张 table :
- 其中包含一个用户列表及其权限:

userId | permission
-------|-----------
1 | build
1 | play
1 | jump
2 | build
2 | jump
2 | run
3 | drink
3 | build
4 | run

-第二个表包含组和他的权限:

groupId | permission
--------|-----------
G1 | build
G1 | jump
G2 | play
G2 | jump
G3 | drink
G3 | run
G4 | drink
G5 | build

我的目标是找到用户可以拥有的所有组:

userId | groupId
-------|-----------
1 | G1
1 | G2
1 | G5
2 | G1
2 | G5
3 | G4
3 | G5

我已创建一个请求来查找哪些用户属于该组,但我无法对所有组执行此操作(我的数据集中有超过 1000 个组):

SELECT DISTINCT userId
FROM (
SELECT userId, count(*) AS nbData
from table_a
WHERE permission in (
SELECT permission
from table_b
where groupId = 'g1'
)
group by userId
) as t
where nbData = (SELECT count(*) from table_b where groupId = 'g1');

如果一个用户拥有该组的所有权限,则该用户属于该组。目标是找到每个用户的每个组

最佳答案

如果两个表中都没有重复项,则可以根据权限将表连接在一起并聚合:

select up.userId, gp.groupId
from user_permissions up join
group_permissions gp
on up.permission = gp.permission
group by up.userId, gp.groupId
having count(*) = (select count(*)
from group_permissions gp2
where gp2.groupId = gp.groupId
);

如果该组的所有权限都匹配,则该用户位于该组中。

关于MySQL:从组描述中重新查找用户组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53752059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com