Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CASE WHEN with OR

there is a way to use OR with CASE WHEN, like this?

SELECT
    case 'brasil'   
    when 'chile' OR 'brasil' THEN   
                        'ok'
    when 'argentina' then
        'ok2'
    when 'venezuela' THEN
        'ok3'
    ELSE
        'chaves'
end;
like image 450
Helio Ferreira Avatar asked Sep 20 '25 00:09

Helio Ferreira


1 Answers

Yes, you are thinking about it correctly. You can use OR or IN with your select..case statement:

select

  case
  -- one way or writing OR
  when country = 'brasil' or country = 'chile' then '....'
  -- another way of writing OR
  when country in ('china', 'japan') then '...'
  else '..'
  end

from tablename;

Example: http://sqlfiddle.com/#!15/c1cef/2

like image 194
zedfoxus Avatar answered Sep 22 '25 18:09

zedfoxus