Thanu
Thanu

Reputation: 2501

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?

Upvotes: 6

Views: 12490

Answers (1)

OMG Ponies
OMG Ponies

Reputation: 332651

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; 

Upvotes: 12

Related Questions