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

邮差的信 提交于 2019-12-08 21:03:46

问题


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?


回答1:


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; 


来源:https://stackoverflow.com/questions/12523630/how-to-write-a-mysql-case-when-statement-with-multiple-search-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!