Mount SQL with options

1

I'm using PostgreSQL on a system of extensions and I have two tables here. One that stores the extensions and the other one that stores the permission to connect these extensions.

This second table is structured like this:

InthesystemImountedafiltertobringtheextensionsaccordingtothefilterappliedbytheuser.Incasethequerybelowisnotreturningmetheexpectedresult:

SELECT p.ramal FROM servico.ramal_permissao_ramal p WHERE p.cod_opcao_permissao_ramal = 'RM' AND p.cod_opcao_permissao_ramal = 'LC'

I would need it to return me to extension 1426 but it does not return anything. I was using the IN but the problem is that the IN does not apply a unique filter.

How can I optimize this query for when the user sets RM and LC to bring me only extensions with those permissions?

Thank you!

    
asked by anonymous 05.02.2018 / 19:12

1 answer

3

You are listing two records in this table. One such as RM and the other with LC . The default way to relate records is with JOIN . Logo:

SELECT p.ramal
FROM servico.ramal_permissao_ramal p
INNER JOIN servico.ramal_permissao_ramal q ON p.ramal = q.ramal
WHERE p.cod_opcao_permissao_ramal = 'RM'
AND q.cod_opcao_permissao_ramal = 'LC'
    
05.02.2018 / 19:36