Case for several fields

Asked

Viewed 209 times

0

I can make a query using 1 case to various fields? The fields are of the type int.

If not, I can do it in php?

Query

select *,
(case (pergunta1,pergunta2)
when 1 then 'Ótimo'
when 2 then 'Bom'
when 3 then 'Regular'
when 4 then 'Ruim'                                                            
END) as pergunta1,pergunta2 from tabela
  • It wouldn’t make the BD return texts that way, it would be unfeasible to work with translation packages, maintenance... Play this in a array in PHP, much simpler.

  • Would that be? $arr = array('', 'Ótimo', 'Bom', 'Regular', 'Ruim'); echo $arr[$pergunta1];

  • Yes, just remove the empty index: $arr = array(1 => 'Ótimo', 2 => 'Bom', 3 => 'Regular', 4=> 'Ruim');

1 answer

1


Yes, but to use this type of case you cannot have the fixed value right after the case and the tests shall be carried out in the when as follows:

select *,
      (case
         when pergunta1 = 1 and pergunta2 = 1 then 'Ótimo'
         when pergunta1 = 2 and pergunta2 = 2 then 'Bom'
         when pergunta1 = 3 and pergunta2 = 3 then 'Regular'
         when pergunta1 = 4 and pergunta2 = 4 then 'Ruim'
      END) as pergunta1,pergunta2 from tabela

Browser other questions tagged

You are not signed in. Login or sign up in order to post.