业务需求
今天获得一个统计报表的业务:
在一个企业用户表中(详细字段不写),现需要统计所有员工的退休日期形成一个定时任务,在定时任务中,如果员工接近还有60天,定时任务会短信发名单给人事部。人事部专员办理退休手续
要求:
1、只显示还有60天就退休的人员信息
2、男的年龄是60岁退休
3、女的年龄是55岁退休(其中工人岗的是50岁退休)
代码如下
select position_hiera,user_name,birth,age,gender,post_type,DATE_ADD(birth,INTERVAL 60 YEAR) retire_date,'21900'-TIMESTAMPDIFF(DAY,birth,now()) as surplus_days from rost_use where gender='男' and '21900'-TIMESTAMPDIFF(DAY,birth,now())>0 and '21900'-TIMESTAMPDIFF(DAY,birth,now())<=60
UNION all
select position_hiera,user_name,birth,age,gender,post_type,DATE_ADD(birth,INTERVAL 50 YEAR) retire_date,'18250'-TIMESTAMPDIFF(DAY,birth,now()) as surplus_days from rost_use where gender='女' and post_type = '工人岗' and '18250'-TIMESTAMPDIFF(DAY,birth,now())>0 and 18250-TIMESTAMPDIFF(DAY,birth,now())<=60
UNION all
select position_hiera,user_name,birth,age,gender,post_type,DATE_ADD(birth,INTERVAL 55 YEAR) retire_date,'20075'-TIMESTAMPDIFF(DAY,birth,now()) as surplus_days from rost_use where gender='女' and post_type not in('工人岗') and '20075'-TIMESTAMPDIFF(DAY,birth,now())>0 and '20075'-TIMESTAMPDIFF(DAY,birth,now())<=60
解析:
-- 出生日期+60年后转换成日期角色
DATE_ADD(birth,INTERVAL 60 YEAR) retire_date
-- 21900 = 60年*365日/年
-- 18250 = 50年*365日/年
-- 20075 = 55年*365日/年
-- mysql函数:【now()-出生日期】转换成天数【DAY】
TIMESTAMPDIFF(DAY,birth,now())
结果如下