gpt4 book ai didi

php - 排除在层次架构中没有父级的数据

转载 作者:行者123 更新时间:2023-12-05 04:27:50 25 4
gpt4 key购买 nike

我在一个项目中工作,我们有几个表存储信息,告诉实体是否可以拥有某些权限。其中一些权限具有层次关系(如果我无法读取此文件,则无法写入文件,例如,权限“write_file”将是“read_file”的子权限)。这就是它的工作原理,多年来一直如此,我无法触及它。

我们可能还有 ooold mysql 版本,比如 mysql 4 :(

我们有一个表“聚合”权限,来自不同的其他表,在聚合之后,我必须在可能影响相当多用户的请求中过滤掉没有父权限的权限。

我不知道如何过滤掉这些权限,这一切:'(

CREATE TABLE permissions {
id int not null,
parentId int not null, -- can be 0, meaning this feature does not have parents
name varchar
-- etc
};

UPDATE aggregat_table at
JOIN toto, titi, tutu -- some joins on other table
INNER JOIN permissions p on p.id = toto.id OR p.id = titi.id OR p.id = tutu.id
-- JOIN on permissions, but I can't figure out how, to filter out parentless permissions
SET -- etc etc

层级不深,多为3层,有时4层。

id;  parent_id;   name

1; 0; "can_drink"
2; 0; "can_walk"
3; 2; "can_run"
5; 4; "can_moonwalk" # is child permission of 4, will be removed
8; 7; "can_write_document" # is child permission of 7; which is child of 6, will be removed
7; 6; "can_read_document" # is child permission of 6, will be removed

如果 permissions p 在上一步中产生,我想保留“can_drink”,因为它是根权限,“can_walk”,同样的原因,“can_run”,因为这些 是他在这个集合中的父项,我想去掉“can_moonwalk”,因为集合中没有他的父项,“can_write_document”和“can_read_document”也必须消失,因为它们依赖于权限 6(can_read_document 有 6 作为父 id,can_write_document 有 can_read_document 作为父,所以有 6 作为祖父)

结果是:

id;  parent_id;   name

1; 0; "can_drink" # is "root" permission, stayed
2; 0; "can_walk" # is "root" permission, stayed
3; 2; "can_run" # is child permission, parent is in the set, stayed

速度很重要。

谢谢你的帮助

最佳答案

(请不要使用旧的“逗号连接”,使用JOIN..ON)

(INNER JOIN == JOIN;我懒得打INNER了)

LEFT JOIN 权限 AS p2 ON ......其中 p2 = 0

UPDATE aggregat_table at
JOIN toto
JOIN titi ON ...
JOIN titu ON ...
# some joins on other table
JOIN permissions p
ON p.id = toto.id
OR p.id = titi.id
OR p.id = tutu.id
WHERE ... [ see below ]
SET ...

要跳过更新没有父项的行:

    WHERE p.parentid != 0

仅对无父行进行操作:

    WHERE p.parentid = 0

要跳过更新没有 GRANDparent 的行:

    WHERE NOT EXISTS( SELECT 1 FROM permissions AS p2
WHERE p2.id = p.parentid
AND p2.parentid = 0 )

并且确保在权限上拥有PRIMARY KEY(id),如果合适的话。如果可能需要 INDEX(parentid)

关于php - 排除在层次架构中没有父级的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72764695/

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