mysql -- 根据变量a的不同值设置变量b(case when + set)

需求:

  根据 变量 @period_endtime 的不同值,设置相对应的 @end_type

@period_endtime @end_type
0331 1
0630 2
0930 3
1231 4

 

实现:

set @i_period = '20230331';

SET @period_endtime = RIGHT(@i_period, 4);

SET @end_type = 
    CASE @period_endtime
        WHEN "0331" THEN 1
        WHEN "0630" THEN 2
        WHEN "0930" THEN 3
        WHEN "1231" THEN 4
    END
;

 

posted on 2023-06-18 15:48  bruce_he  阅读(23)  评论(0编辑  收藏  举报