Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a MYSQL CASE WHEN statement with multiple search conditions?

I know languages like PHP has switch case control structure that supports multiple validations in a single case statement like,

Switch $x {
  case 1,2,3: 
      $a = 0;
       break;
  case 5,6: 
       $a = 1;
       break;
}

Similarly can this be done in MYSQL? I tried below, which really didn't work though :(

CASE vc_shape
   WHEN ('02' OR '51') THEN SET dc_square_1 = dc_square_1 + dc_row_total;
   WHEN ('06' OR  '30' OR 83) THEN SET dc_square_2 = dc_square_2 + dc_row_total; 
   .....
   .....
  ELSE
      BEGIN
      END;
END CASE; 

Any ideas how can I achieve this?

like image 647
Thanu Avatar asked Sep 21 '12 02:09

Thanu


1 Answers

Use the other format for CASE statements:

CASE 
   WHEN vc_shape IN ('02', '51') THEN SET dc_square_1 = dc_square_1 + dc_row_total;
   WHEN vc_shape IN ('06', '30', '83') THEN SET dc_square_2 = dc_square_2 + dc_row_total; 
   .....
   .....
  ELSE
      BEGIN
      END;
END CASE; 
like image 74
OMG Ponies Avatar answered Sep 19 '22 10:09

OMG Ponies