查询管理员级别访问权限

时间:2012-02-15 20:59:34

标签: mysql admin

表:

Admin
Id | User | Pass

Modules
Id | Name (ex. users, admin, gallery )

Permissions
Id | Name (ex. add, delete, read, update)

Levels
Id | Module (ex. gallery) | Cod_Permission (ex. 10) | Administrator (ex. user123)

所以我有疑问:

SELECT
 n.Modulo,
 p.Nome AS Tipo,
 n.Administrador AS Nome,
 n.Cod_Permissao
 FROM
 niveis AS n
 Inner Join modulos AS m ON n.Modulo = m.Nome
 Inner Join permissoes AS p ON n.Cod_Permissao = p.Codigo
 WHERE
 n.Administrador =  'user123'

返回:

Id -> Module -> Cod_Permission -> Administator
 1    gallery    1    user123
 2    gallery    2    user123

我希望将同一模块的所有结果分组在一行中,如此

Id -> Module -> Read -> Update -> Delete -> Insert -> Username
 1   gallery    1    0    0    4    user123

如何制作?

2 个答案:

答案 0 :(得分:0)

像这样 -

SELECT
    n.Modulo,
    p.Nome AS Tipo,
    n.Administrador AS Nome,
    SUM(IF(n.Cod_Permissao = 1, 1, 0)) AS Read,
    SUM(IF(n.Cod_Permissao = 2, 1, 0)) AS Update,
    SUM(IF(n.Cod_Permissao = 3, 1, 0)) AS Delete,
    SUM(IF(n.Cod_Permissao = 4, 1, 0)) AS Insert
FROM niveis AS n
INNER JOIN modulos AS m
    ON n.Modulo = m.Nome
INNER JOIN permissoes AS p
    ON n.Cod_Permissao = p.Codigo
WHERE n.Administrador = 'user123'
GROUP BY
    n.Modulo,
    p.Nome,
    n.Administrador

如果不确切知道不同的字段是什么,我不能100%确定,但这应该让你朝着正确的方向前进。

答案 1 :(得分:0)

nnichols很好的dudeeee!

解: 同样的查询你的,只改变:

GROUP BY
    n.Modulo,
    p.Nome,
    n.Administrador

GROUP BY
    n.Modulo,
    n.Administrador
SELECT
    n.Modulo,
    p.Nome AS Tipo,
    n.Administrador AS Nome,
    SUM(IF(n.Cod_Permissao = 1, 1, 0)) AS Ler,
    SUM(IF(n.Cod_Permissao = 2, 2, 0)) AS Atualizar,
    SUM(IF(n.Cod_Permissao = 3, 3, 0)) AS Excluir,
    SUM(IF(n.Cod_Permissao = 4, 4, 0)) AS Inserir
FROM niveis AS n
INNER JOIN modulos AS m
    ON n.Modulo = m.Nome
INNER JOIN permissoes AS p
    ON n.Cod_Permissao = p.Codigo
WHERE n.Administrador = 'kin'
GROUP BY
    n.Modulo,
    n.Administrador