不明白这个SQL查询

时间:2013-06-12 19:05:03

标签: mysql sql inner-join

我试图从CakePHP 2.3应用程序中理解这个查询,该应用程序应该在一次查找中获得所有ACL权限。我很难跟踪联接和" lft" " rght"节点

SELECT
        theAro.id as AroId,
        theAro.parent_id as AroParentId,
        theAro.alias as AroAlias,
        theAro.model as AroModel,
        theAco.id as AcoId,
        theAco.parent_id as AcoParentId,
        theAco.alias as AcoAlias,
        theAco.rght as AcoRight,
        permissions._create as CanCreate,
        permissions._read as CanRead,
        permissions._update as CanEdit,
        permissions._delete as CanDelete
        FROM acos AS theAco, aros AS theAro INNER JOIN aros_acos AS permissions ON
        (permissions.id =
            (SELECT permissions.id FROM aros_acos AS permissions INNER JOIN acos AS ruleAco ON
                (ruleAco.id = permissions.aco_id) INNER JOIN aros AS ruleAro ON
                (permissions.aro_id = ruleAro.id)
                WHERE ruleAco.lft <= theAco.lft
                AND ruleAco.rght >= theAco.rght
                AND theAro.lft >= ruleAro.lft
                AND theAro.rght <= ruleAro.rght
                ORDER BY
                ruleAro.lft DESC,
                ruleAco.lft DESC LIMIT 1
            )
        )WHERE theAco.id = '$actionAcoId' AND permissions._create = 1 ORDER BY theAro.lft ASC

1 个答案:

答案 0 :(得分:1)

如果可以的话,打开一个sql命令行并尝试运行最简单的查询形式,例如选择theAro.id作为AroId FROM aros AS theAro;现在尝试以逻辑方式连续添加查询的每个路径,重新创建原始查询并记下它在此过程中的作用。即,在重新创建整个查询之前,当您添加更多条件时,哪些数据会消失什么数据?