用mysql解析crontab表达式

SELECT

  `cron` as `执行周期规则`,

  concat(

  if(

  locate('/',SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 5), ' ', -1))>0,

  concat('每',replace(SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 5), ' ', -1),'*/',''),'月'),

  if(SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 5), ' ', -1)='*','',concat(SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 5), ' ', -1),'月'))

  ),

  if(

  locate('/',SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 4), ' ', -1))>0,

  concat('每',replace(SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 4), ' ', -1),'*/',''),'天'),

  if(SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 4), ' ', -1)='*','每天',concat(SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 4), ' ', -1),'日'))

  ),

  if(

  locate('/',SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 3), ' ', -1))>0,

  concat('每',replace(SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 3), ' ', -1),'*/',''),'小时'),

  if(SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 3), ' ', -1)='*'||SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 3), ' ', -1)='0','',concat(SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 3), ' ', -1),'时'))

  ),

  if(

  locate('/',SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 2), ' ', -1))>0,

  concat('每',replace(SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 2), ' ', -1),'*/',''),'分钟'),

  if(SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 2), ' ', -1)='*'||SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 2), ' ', -1)='0','',concat(SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 2), ' ', -1),'分钟'))

  ),

  if(

  locate('/',SUBSTRING_INDEX(`cron`, ' ', 1))>0,

  concat('每',replace(SUBSTRING_INDEX(`cron`, ' ', 1),'*/',''),'秒'),

  if(SUBSTRING_INDEX(`cron`, ' ', 1)='*'||SUBSTRING_INDEX(`cron`, ' ', 1)='0'||SUBSTRING_INDEX(`cron`, ' ', 1)='00','',concat(SUBSTRING_INDEX(`cron`, ' ', 1),'秒'))

  )

  )as `频率`

    -- concat(SUBSTRING_INDEX(`cron`, ' ', 1),'秒') AS seconds,

    -- SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 2), ' ', -1) AS minutes,

    -- SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 3), ' ', -1) AS hours,

    -- SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 4), ' ', -1) AS day_of_month,

    -- SUBSTRING_INDEX(SUBSTRING_INDEX(`cron`, ' ', 5), ' ', -1) AS month,

    -- SUBSTRING_INDEX(`cron`, ' ', -1) AS day_of_week,

FROM

(

(select "0 3 * * *" as cron)

union all

(select "0 */4 * * *" as cron)

union all

(select "11 17 1 * *" as cron)

union all

(select "11 9,11,13 2 * *" as cron)

union all

(select "01 9 * * *" as cron)

union all

(select "01 9 * * *" as cron)

) a

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。