Q:

Query for Admin Levels access

Tables:
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)

So an I have query:
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'

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

I want group all results for same module in one line like this
Id -> Module -> Read -> Update -> Delete -> Insert -> Username
 1   gallery    1    0    0    4    user123

How to make this?

A:

Something like this -
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

Without knowing exactly what the different fields are I cannot be 100% sure but this should get you heading in the right direction.

