无法访问内部作用域中的变量?

时间:2018-06-24 17:05:07

标签: mysql sql

我正在尝试使此SQL查询正常工作。问题是我无法在其参数中的内容中访问core_user.id。我真的不知道为什么。

SELECT
    core_user.id AS target, (
        SELECT
            COUNT(permission_id) AS permissions
        FROM (
            SELECT
                DISTINCT permission_id
            FROM (
                SELECT
                    id,
                    permission_id
                FROM
                    core_user_user_permissions
                WHERE
                    core_user_user_permissions.user_id = core_user.id
                UNION
                SELECT
                    id,
                    permission_id
                FROM
                    auth_group_permissions
                WHERE
                    auth_group_permissions.group_id IN (
                        SELECT
                            group_id
                        FROM
                            core_user_groups
                        WHERE
                            core_user_groups.user_id = core_user.id)) AS `permissions`) AS `derived`) AS `perms`
        FROM
            `core_user`
        WHERE
            `core_user`.`is_active` = TRUE
        GROUP BY
            `core_user`.`id`
        ORDER BY
            `perms` ASC

如果我尝试将target.idcore_user AS target一起使用,则它也不起作用:Unknown column 'target.id' in 'where clause'

预期结果:

+----------------------------------+-------+
| id                               | perms |
+----------------------------------+-------+
| ab7ec54bf9124dffb807fb89f9ea8036 |    0 |
| b54d4d3f97134dfcbc36ac193c0c1250 |    81 |
| c69ffa4d162b49129ff6a316da3caaa3 |    64 |
| f8ac73eee80044359c246f3b173aa631 |    0 |
+----------------------------------+-------+

有什么解决方法吗?

3 个答案:

答案 0 :(得分:1)

如果只有一个(无效的)查询,而没有描述应检索的内容,则不容易理解一个人想要什么。因此,这只是一个最佳猜测:

SELECT cu.id target,
       count(x.permission_id) perms
       FROM core_user cu
            LEFT JOIN (SELECT cuup.permission_id,
                              cuup.user_id
                              FROM core_user_user_permissions cuup
                              WHERE cuup.permission_id IS NOT NULL
                       UNION
                       SELECT agp.permission_id,
                              cug.user_id
                              FROM auth_group_permissions agp
                                   INNER JOIN core_user_groups cug
                                              ON cug.group_id = agp.group_id
                              WHERE agp.permission_id IS NOT NULL) x
                      ON x.user_id = cu.id
       WHERE cu.is_active = TRUE
       GROUP BY cu.id
       ORDER BY perms ASC;

与其在子选择中获得计数,不如将其留下来,而是加入每个用户的权限ID。按用户ID进行分组,然后获得用户非null权限ID的计数(不同,因为联合已经删除了所有重复项,并且idcore_user(我假设)中也是唯一的),是我相信你想要的。

(如果列上没有空约束,则可以在内部查询中删除WHERE <alias>.permission_id IS NOT NULL。这里的想法是,无论如何都不会在最后计算空值,因此我们要丢弃它们尽早。)

(未经测试,因为未提供架构或示例数据。可能包含错字。)

答案 1 :(得分:0)

我将其重写为:

SELECT core_user.id AS target,
   (SELECT COUNT(permission_id) AS permissions
    FROM (SELECT permission_id
          FROM core_user_user_permissions
          WHERE core_user_user_permissions.user_id = `core_user`.id
          UNION
          SELECT permission_id
          FROM auth_group_permissions
          WHEREauth_group_permissions.group_id IN (
                        SELECT group_id
                        FROM core_user_groups
                        WHERE core_user_groups.user_id = `core_user`.id)
          ) AS `derived`
  ) AS `perms`
FROM `core_user`
WHERE  `core_user`.`is_active` = TRUE
GROUP BY `core_user`.`id`
ORDER BY `perms`;

我已在SELECT列表中删除了一级嵌套子查询。

答案 2 :(得分:0)

我希望我可以通过排除小组前提来获得您的预期结果:

SELECT core_user.id AS target, 
    (
        SELECT COUNT(DISTINCT(permission_id)) AS permissions
        FROM 
        (
            SELECT cuup.id as id, cuup.permission_id as permission_id
            FROM core_user_user_permissions cuup
            -- WHERE cuup.user_id = cu.id  
            --VV-- this should be a join
            JOIN core_user cu ON cu.id = cuup.user_id
        )
    ) AS `perms`
FROM `core_user`
WHERE `core_user`.`is_active` = TRUE
GROUP BY `core_user`.`id`
ORDER BY `perms` ASC